C++ base constructor call vs. D's

kinke noone at nowhere.com
Wed Oct 2 22:48:18 UTC 2019


On Wednesday, 2 October 2019 at 17:22:40 UTC, Just Dave wrote:
> I was reading the C++ to D page, and came across this little 
> bit about when to call the base class constructor:
>
>
> Isn't there some inherent danger of not calling the base 
> constructor first?

The object's fields are pre-initialized before invoking the 
constructor, and not undefined as in C++, so probably not really 
dangerous.

> "It's superior to C++ in that the base constructor call can be 
> flexibly placed anywhere in the derived constructor."

That formulation is s bit suboptimal, as the emphasis should be 
on '*can* be flexibly placed anywhere' - if you don't specify an 
explicit `super()` call, it's implicitly inserted at the 
beginning of the derived ctor, so a base ctor is always invoked 
at some time when constructing a derived object.
I don't think there are lots of use cases for deferring the super 
call, but it might be useful for logging purposes and such.


More information about the Digitalmars-d-learn mailing list