On Thursday, January 20, 2011 17:18:48 Luke J. West wrote:
> Hi to all from a total noob.
> 
> first of all, I'd like to say how impressed I am with D. In fact, I keep
> pinching myself. Have I *really* found a language worth leaving C++ for
> after two decades? It's beginning to look that way. Obviously I'm
> devouring the 2.0 documentation right now, but have not yet found out
> how to create a new instance of an existing class object. What I mean
> is, given...
> 
> auto a = new A;
> 
> how do I, in c++ speak, do the D for...
> 
> A  b(a); // or if you prefer...
> A* b = new A(a);
> 
> I'm sure this must be trivial.
> 
> Many many thanks,

Well, you could create a constructor which is effectively a copy constructor. 
There has been talk of a general cloning mechanism which would give you a 
correct clone method essentially for free, but it hasn't been done yet (not 
sure 
why; I don't recall exactly where the last discussion on that went other than 
it 
was generally accepted that we wanted something like that). However, at the 
moment, I'm not aware of any general way to copy a class instance like you're 
trying to do.

Structs have postblit constructors:

this(this)
{
   //...
}

where a shallow copy of the struct is made prior to entering this(this), and 
then you do whatever you need to do inside it to make it a deep copy (or just 
don't have a postblit constructor if you don't need a deep copy). However, 
classes are reference types and don't have anything like that.

So, we need to add a means to generate clone methods for classes so that 
copying 
classes will be easy, but we haven't done it yet. I'm not sure what the current 
obstacles to doing it are.

- Jonathan M Davis

Reply via email to