www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Copy constructor in D. Why it is necessary to have it.

reply Eldar Insafutdinov <e.insafutdinov gmail.com> writes:
Denis Koroskin Wrote:

 On Tue, 30 Sep 2008 23:37:50 +0400, Eldar Insafutdinov  
 <e.insafutdinov 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();


Sep 30 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
Eldar Insafutdinov:
     auto itCopy = it.clone();



In D all objects are managed by reference. So when you copy automatically, you are just copying its reference, that's a pointer fully managed by the GC. If you want a copy of the data of an object you ask so to it. This simplifies collections and reduces the number of copies, improving performance. Instead of "clone()" I suggest "dup", that's the standard in D. Bye, bearophile
Sep 30 2008
next sibling parent BCS <ao pathlink.com> writes:
Reply to bearophile,

 Eldar Insafutdinov:
 
 auto itCopy = it.clone();
 


appropriate keyword. I should only write a copy constructor for my data and let to do the rest to the compiler.


BTW note that structs ARE pass by value.
Sep 30 2008
prev sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
On Wed, Oct 1, 2008 at 5:34 AM, bearophile <bearophileHUGS lycos.com> wrote:
 Eldar Insafutdinov:
     auto itCopy = it.clone();



In D all objects are managed by reference. So when you copy automatically, you are just copying its reference, that's a pointer fully managed by the GC. If you want a copy of the data of an object you ask so to it. This simplifies collections and reduces the number of copies, improving performance. Instead of "clone()" I suggest "dup", that's the standard in D.

It has been pointed out though, that dup is a "shallow copy" in D's built-in usage. If you want a deep copy operation, there is no precedent in the base language, I believe. Clone is a good one to standardize on for deep copies, I think. --bb
Sep 30 2008