Class member function calls inside ctor and dtor

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Jan 27 17:01:24 UTC 2018


On Saturday, January 27, 2018 16:18:26 Thomas Mader via Digitalmars-d wrote:
> On Saturday, 27 January 2018 at 14:48:08 UTC, Johan Engelen wrote:
> > I'm working on devirtualization and for that it's crucial to
> > know some spec details (or define them in the spec if they
> > aren't yet).
> >
> > Currently, calling non-final member functions in the destructor
> > is allowed and these indirect calls are to the possibly
> > overridden functions of derived classes. That is, inside a base
> > class constructor, the object's type is its final type (so
> > possibly of a derived class). This is the same in the
> > destructor. Thus, the object's dynamic type does not change
> > during its lifetime.
>
> Can't answer your question but have a little question.
> How is the behavior different to the situation in C++? They argue
> that it's not good to call virtual methods in Con-/Destructors in
> https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-ctor-virtu
> al
>
> So I guess it should better be not used in D as well?

D solved that problem. In C++, when you're in the base class constructor,
the object doesn't have its derived type yet. It's still just the base
class. Each class level gets add as it's constructed (the same in reverse
with the destructor). You don't have a full object until all constructors
have been run, and once you start running destructors, you don't have a full
class anymore either.

In D, on the other hand, the object is initialized with its init value
_before_ any constructors are run. So, it's a full object with a full type,
and everything virtual is going to get the type right.

- Jonathan M Davis



More information about the Digitalmars-d mailing list