Asserting that a base constructor is always called

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun May 24 07:42:26 UTC 2020


On Sunday, May 24, 2020 12:38:46 AM MDT Tim via Digitalmars-d-learn wrote:
> Oh right. I mean it makes sense but I got confused when super()
> is valid syntax. Why would you need to call the super constructor
> when it's called automatically?

1. If you wanted to run any code before calling the base class constructor.
e.g. depending on the arguments to the derived class constructor, you could
call one base constructor, or you could call a different base class
constructor. You could even have the base class constructor call a virtual
function, and what that virtual function did depended on something you had
set in the derived class prior to calling the base class constructor (since
unlike C++, it's safe to call virtual functions from within constructors).

2. If the base class constructor is not a default constructor, then you have
to explicitly call it, because it has to be passed arguments.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list