move object from heap to stack

monarch_dodra monarchdodra at gmail.com
Thu Sep 20 03:37:42 PDT 2012


On Thursday, 20 September 2012 at 10:20:24 UTC, Johannes Pfau 
wrote:
> http://dlang.org/struct.html
> D classes do not really have copy constructors. You could 
> assume that
> if a constructor has only one argument and it's of the same 
> type as the class, it's a copy constructor.

True... the term "copy constructor" is not 100% accurate, it is 
just "a constructor that takes the same type"

A a = new A();  //Creates a new A
A b = a;        //Binds to the existing A
A c = new A(a); //Constructs a new A, form an old A.

> But that's a pure convention then,
> nothing than could be enforced in any way. Copying classes is
> dangerous, and problems can also happen without constructors:
> [SNIP]

Yes, that is a good point. I could find ways to make it crash 
even with a POD class, so I guess we could relax the condition to 
"don't ever memcpy a class: They aren't designed for that. Bad! 
Bad! Don't do it!"

I'm learning from this thread.


More information about the Digitalmars-d-learn mailing list