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

bearophile bearophileHUGS at lycos.com
Tue Sep 30 13:34:58 PDT 2008


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



More information about the Digitalmars-d mailing list