what was wrong with struct & class in C++?

Yigal Chripun yigal100 at gmail.com
Sun Dec 16 13:55:18 PST 2007


Bill Baxter wrote:
> Bill Baxter wrote:
>> Bill Baxter wrote:
>>> Yigal Chripun wrote:
> 
>> There is one problem with that solution.  classinfo.create only works 
>> if the object being duplicated has a default constructor.  If you 
>> split it into dup and copy, then you can have the most derived class 
>> create the instance, calling whatever constructor it wants to.
>>
>> I'm not sure why classinfo.create returns null though.  The doc for it 
>> only says "Create instance of Object represented by 'this'.".  Doesn't 
>> mention anything about ever returning null.
> 
> Another problem is that it's far too easy for derived classes to forget 
> that they need to implement their own dup(), so you end up with what's 
> effectively another variation of the slicing problem.  You get an object 
> that's a Derived but for some reason all the Derived-specific members 
> are bogus.  I suppose it's not quite as bad as the real slicing problem 
> though, because once detected it can always be fixed at the source, 
> whereas with slicing, the line of code needing fixing could be anywhere.
> 
> And that problem exists with the split dup/copy solution too.
> 
> --bb

here's a guide about securing Java code, it has a paragraph related to 
this discussion about dup (clone in java)
http://www.securingjava.com/chapter-seven/chapter-seven-1.html
I think it applies to D as well.
another solution to this problem is a copy constructor as in c++.
( it should be only provided by the class designer so no compiler 
generated copy c-tors, and should be explicitly called by the client code.)
also I think that const in D will remove a large portion of the need for 
dup because you could pass a const reference to your object instead of 
dupping it.



More information about the Digitalmars-d mailing list