inheriting constructos

Don nospam at nospam.com
Mon Nov 30 00:30:25 PST 2009


Andrei Alexandrescu wrote:
> Walter and I just discussed the matter of inheriting constructors. Our 
> thought on the issue:
> 
> a) If a class doesn't define any constructors and adds no fields, 
> inherit constructors. Example:
> 
> class MyException : Exception {}
> 
> b) If a class defines at least one constructor, do not inherit 
> constructors.
> 
> c) If a class doesn't define any constructors but does add at least a 
> non-static field -> undecided.
> 
> What do you think?
> 
> Andrei

I would add: "and the class does not define a class invariant".

A constructor exists to establish the class invariant (even if that 
invariant is implicit). For (a), the old invariant will still be satisfied.
In the case (c) there is a chance that the invariant will not be 
appropriate. In (c), I suggest that it is valid to inherit constructors 
if and only if the new class is a simple aggregate of the old class, 
together with the new members (which may have invariants of their own).
If there's no constructor and no invariant, then either it's a simple 
aggregate, or it's a bug.





More information about the Digitalmars-d mailing list