Copy constructor in D. Why it is necessary to have it.

Eldar Insafutdinov e.insafutdinov at gmail.com
Tue Sep 30 14:01:35 PDT 2008


bearophile Wrote:

> Eldar Insafutdinov:
> > >>     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.
> 
> 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.



More information about the Digitalmars-d mailing list