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

guslay guslay at gmail.com
Sun Dec 16 15:38:44 PST 2007


Bill Baxter Wrote:

> 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


It would be nice to have an attribute to say "this method must be implemented or re-implemented in the most derived class". It's basically the opposite of final, it's a must-override. This would be very useful when implementing cloning, serialization.

For instance, if you derive class B from a concrete class A, but fail to provide a reimplementation of dup(), class B will be abstract.

There are other ways to do this (never derive from concrete class, always carry the parent's interface when inheriting, documentation), but they don't offer strong guarantees.





More information about the Digitalmars-d mailing list