Copy constructor in D. Why it is necessary to have it.
Eldar Insafutdinov
e.insafutdinov at gmail.com
Tue Sep 30 13:18:13 PDT 2008
Denis Koroskin Wrote:
> On Tue, 30 Sep 2008 23:37:50 +0400, Eldar Insafutdinov
> <e.insafutdinov at gmail.com> wrote:
>
> > http://www.everfall.com/paste/id.php?m46jrb36o7qu
> >
> > This is a short example. It has all the comments inside. Since "in"
> > keyword tells the compiler to make a copy of an class object - a new
> > object is created.
>
> No, that's not true. No heap activity is done under the hood, a copy of
> /pointer/ is passed (as opposed to a copy of /instance/).
> In means that changes to the variable won't be visible to the caller:
In my particular case changes are made to the data behind the pointer - and they are not copied because, only pointer itself is copied. In C++ when you call a function like this:
foo(TClass instance);
copy constructor is called to create a proper copy of an object. "in" keyword means exactly the same I guess? it makes a local copy of an object, so that variable is not supposed to be modified. But in my case since I use a binding to a C-library - the real data is behind the pointer. foo() cannot modify the class fields themself, but since pointer both in variable and local copy point to the same data - the semantics of "in" doesn't work.
>> auto itCopy = it.clone();
Why copy it manually if language tends to do it so by providing appropriate keyword. I should only write a copy constructor for my data and let to do the rest to the compiler.
More information about the Digitalmars-d
mailing list