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:
bearophile 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. Bye, bearophile

Ah ok, so when using "in" with class objects - it means that I can't modify the reference itself. So no copy occured. I was wrong.
Sep 30 2008
parent "Bill Baxter" <wbaxter gmail.com> writes:
On Wed, Oct 1, 2008 at 6:01 AM, Eldar Insafutdinov
<e.insafutdinov gmail.com> wrote:
 bearophile 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. Bye, bearophile

Ah ok, so when using "in" with class objects - it means that I can't modify the reference itself. So no copy occured. I was wrong.

In D1 'in' is a no-op. It just means pass the parameter normally. Which for classes means you're actually passing a pointer/reference to the class. I've done very little with D2, but in D2 I think 'in' on a parameter means 'const'. Or maybe "const final". --bb
Sep 30 2008