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

Bill Baxter wbaxter at gmail.com
Tue Sep 30 13:39:55 PDT 2008


On Wed, Oct 1, 2008 at 5:34 AM, bearophile <bearophileHUGS at lycos.com> 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.
>

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



More information about the Digitalmars-d mailing list