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

Bill Baxter dnewsgroup at billbaxter.com
Sun Dec 16 16:19:23 PST 2007


guslay wrote:
> 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.

Or like 'abstract' that never gets turned off.  "super abstract"!

(Stolen from Walter's original idea of "super const" for what is now 
called "invariant" in D2 ;-))

But seriously, the feature sounds useful, and like it would be not too 
difficult to implement.

> 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.

I agree.  D is supposed to be about preventing you from making mistakes 
like that.

--bb



More information about the Digitalmars-d mailing list