Inherited constructor

Luna Nielsen luna at foxgirls.gay
Sun Jul 27 12:57:51 UTC 2025


On Friday, 25 July 2025 at 03:57:34 UTC, Andy Valencia wrote:
> On Friday, 25 July 2025 at 02:40:35 UTC, H. S. Teoh wrote:
>
>> In D, constructors are not inherited, so yes, unfortunately 
>> you have to write a forwarding ctor that passes the arguments 
>> along to the base class.
>
> My OO worldview goes back to Smalltalk, where constructors are 
> "just" methods and thus you can reason about them as you would 
> any other inherited method.  I see the point of nudging 
> explicit treatment of superclass constructors.  Is it nannying? 
>  A bit.  Nothing that would make me walk away from D.  And I 
> appreciate this extra insight into how D looks at constructors.
>
> Thank you--both of you!
> Andy

For a more technical explanation; in D constructors don’t live in 
the class vtable. So they can never be virtual. As such 
constructors are only OOP at compile time. This is why you need 
to call super constructors explicitly; otherwise you could end up 
corrupting state of objects you’ve inherited from.

Constructors do have entries in the Runtime Type Info 
(TypeInfo_Class) but those are seperate static instances from the 
overall class vtable (though vtable entry 0 points to the type 
info usually)

You can of course also break the type system if you really want 
to; but I’d recommend against doing so.



More information about the Digitalmars-d-learn mailing list