Implementing .dup / clone for class hierarchies

Steven Schveighoffer schveiguy at yahoo.com
Mon Dec 10 07:07:58 PST 2007


If you have dup return an Object, then you can use the classinfo to allocate 
a new object of the most derived type (I think), and then just override the 
copy method.  The only issue is casting once you get the result, I like how 
arrays don't need to be casted on dup.  But this could be overcome with a 
wrapper dup that calls the base method and just casts the return.  Ugly, but 
doable.

One issue with your implementation, if I have a DerivedA reference to a 
DerivedB class, and I call dup, I'm going to get a DerivedA class only, 
wouldn't I want a complete copy (i.e. a DerivedB instance)?

-Steve 




More information about the Digitalmars-d-learn mailing list