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

Bill Baxter wbaxter at gmail.com
Tue Sep 30 14:09:20 PDT 2008


On Wed, Oct 1, 2008 at 6:01 AM, Eldar Insafutdinov
<e.insafutdinov at gmail.com> wrote:
> 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.

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



More information about the Digitalmars-d mailing list