Virtual method call from constructor

Johan j at j.nl
Wed Apr 5 06:06:43 UTC 2023


On Tuesday, 4 April 2023 at 07:08:52 UTC, Chris Katko wrote:
> dscanner reports this as a warning:
>
> ```D
> struct foo{
> this()
>   {
>   /* some initial setup */
>   refresh();
>   }
> void refresh() { /* setup some more stuff */}
> // [warn] a virtual call inside a constructor may lead to 
> unexpected results in the derived classes
>
> }
> ```
>
> Firstly, are all calls virtual in a struct/class in D? Is this 
> any different from C++?

Regarding this warning, the big difference between C++ and D 
classes is that a D class object is of type Child its whole life 
including during the Base class constructor execution. In C++ a 
class object starts as Base, Base::constructor is executed, then 
type is changed to Child (implemented by overwriting the vtable 
pointer), then Child::constructor is executed.

-Johan


More information about the Digitalmars-d-learn mailing list