Sujet : Re: That depends... (Was: Regarding assignment to struct)
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 03. May 2025, 06:11:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250502220618.480@kylheku.com>
References : 1 2 3 4 5
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-05-03, Kaz Kylheku <
643-408-1753@kylheku.com> wrote:
- some structures cannot be copied; duplicating objects can
be nontrivial when they manage resources or are sensitive
to their own address (like have pointers to parts of themselves).
E.g. you can't just have a FILE parameter and pass (*stdout) to it.
Under object-based/oriented programming in C, we usually pass
around pointers to objects. Treating OOP objects by value requires
techniques found in C++: you need handlers to be invoked when
objects are copied ("copy constructors").
Expanding on this point, C++ goes to town with pass-by-value interfaces
involving objects. For instance the std::basic_string template allows
C++ programs to treat dynamic strings as just values to be tossed
around:
std::string path_combine(std::string dir, std::string name)
{
return dir + "/" + name;
}
C++ experience informs us that you can get away with doing this
with PODs (plain old datatypes---structs with no constructors
or destructors) only to a point. But in C, that's all you have.
You are limited in C by not being able to endow the type with
the brains for what to do when it is copied: like for instance
to share a pointer to some associated data with the new copy,
bumping up a reference count.
That tends to exert "downward pressure" on the technique.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca