why virtual base dtor gets called?

Is it correct that the virtual dtor of base gets called implicitly?
Here's some code to demonstrate what I mean:
Class B has a virtual destructor, so has class D which
is derived from B. Deleting D calls the dtor of D and
then the dtor of B.
I was thinking that this would be true only for non-virtual dtor case,
but I wouldn't have expected it happen for a virtual dtor.
For a class with a virtual dtor I would have expected that only
the dtor of D would be called when D gets deleted.
What's correct? Is maybe my compiler buggy?

Advertisements

"tuvok" <> wrote in message
news:d8im0o$ohu$00$-online.com...
> Is it correct that the virtual dtor of base gets called implicitly?

Not really, the virtual d~tor of base class is invoked, not called.
> Here's some code to demonstrate what I mean:
> Class B has a virtual destructor, so has class D which
> is derived from B. Deleting D calls the dtor of D and
> then the dtor of B.

B's d~tor is invoked and processed before D's d~tor can complete.
> I was thinking that this would be true only for non-virtual dtor case,
> but I wouldn't have expected it happen for a virtual dtor.
> For a class with a virtual dtor I would have expected that only
> the dtor of D would be called when D gets deleted.

Peter Julian wrote:
> tuvok wrote:
>> For a class with a virtual dtor I would have expected that only
>> the dtor of D would be called when D gets deleted.
>
> You are describing a non-virtual d~tor. Only D's d~tor would be
> invoke. Which is the crux of the arguement of why a derived class
> needs the base to have the virtual d~tor.

"Peter Julian" wrote
>
> "tuvok" wrote in message
>
> > Is it correct that the virtual dtor of base gets called implicitly?
>
> Not really, the virtual d~tor of base class is invoked, not called.
>
> > Here's some code to demonstrate what I mean:
> > Class B has a virtual destructor, so has class D which
> > is derived from B. Deleting D calls the dtor of D and
> > then the dtor of B.
>
> B's d~tor is invoked and processed before D's d~tor can complete.
>
> > I was thinking that this would be true only for non-virtual dtor case,
> > but I wouldn't have expected it happen for a virtual dtor.
> > For a class with a virtual dtor I would have expected that only
> > the dtor of D would be called when D gets deleted.
>
> You are describing a non-virtual d~tor. Only D's d~tor would be invoke.

No, it's not the case. Both ~D and ~B are invoked no matter whether
the dtors are virtual or not. So there seems no difference whether
the dtors are virtual or not. Ie. always the dtors of all parts get invoked.
> Which is the crux of the arguement of why a derived class needs the base to
> have the virtual d~tor.

"Old Wolf" <> wrote in message
news:...
> Peter Julian wrote:
> > tuvok wrote:
> >> For a class with a virtual dtor I would have expected that only
> >> the dtor of D would be called when D gets deleted.
> >
> > You are describing a non-virtual d~tor. Only D's d~tor would be
> > invoke. Which is the crux of the arguement of why a derived class
> > needs the base to have the virtual d~tor.
>
> Please don't answer questions here if you don't know what
> you are talking about. Invoking a derived class's destructor
> ALWAYS calls the base class destructors.

He said "when D gets deleted". Please don't criticize my comments if you
can't even read the post. Incidentally, the base destructor isn't called,
its invoked. That means that you can't overide the base d~tor, in case you
are wondering what the difference is.

In the case a pointer to D is was involved, B's d~tor would not be invoked.

While my example didn't include a pointer to the derived class, the output
doesn't produce the warning.

"tuvok" <> wrote in message
news:d8jaa5$48a$03$-online.com...
> "Peter Julian" wrote
> >
> > "tuvok" wrote in message
> >
> > > Is it correct that the virtual dtor of base gets called implicitly?
> >
> > Not really, the virtual d~tor of base class is invoked, not called.
> >
> > > Here's some code to demonstrate what I mean:
> > > Class B has a virtual destructor, so has class D which
> > > is derived from B. Deleting D calls the dtor of D and
> > > then the dtor of B.
> >
> > B's d~tor is invoked and processed before D's d~tor can complete.
> >
> > > I was thinking that this would be true only for non-virtual dtor case,
> > > but I wouldn't have expected it happen for a virtual dtor.
> > > For a class with a virtual dtor I would have expected that only
> > > the dtor of D would be called when D gets deleted.
> >
> > You are describing a non-virtual d~tor. Only D's d~tor would be invoke.
>
> No, it's not the case. Both ~D and ~B are invoked no matter whether
> the dtors are virtual or not. So there seems no difference whether
> the dtors are virtual or not. Ie. always the dtors of all parts get
invoked.
>

In the case where a pointer isn't involved, the base's d~tor will still be
invoked but you'll have the warning. You said "when D gets deleted". In the
case you had a pointer, something i didn't include in my example, your base
c~tor would not have been invoked.

"Peter Julian" <> wrote in message
news:Rfire.1619$...
>
> "tuvok" <> wrote in message
> news:d8jaa5$48a$03$-online.com...
>> "Peter Julian" wrote
>> >
>> > "tuvok" wrote in message
>> >
>> > > Is it correct that the virtual dtor of base gets called implicitly?
>> >
>> > Not really, the virtual d~tor of base class is invoked, not called.
>> >
>> > > Here's some code to demonstrate what I mean:
>> > > Class B has a virtual destructor, so has class D which
>> > > is derived from B. Deleting D calls the dtor of D and
>> > > then the dtor of B.
>> >
>> > B's d~tor is invoked and processed before D's d~tor can complete.
>> >
>> > > I was thinking that this would be true only for non-virtual dtor
>> > > case,
>> > > but I wouldn't have expected it happen for a virtual dtor.
>> > > For a class with a virtual dtor I would have expected that only
>> > > the dtor of D would be called when D gets deleted.
>> >
>> > You are describing a non-virtual d~tor. Only D's d~tor would be invoke.
>>
>> No, it's not the case. Both ~D and ~B are invoked no matter whether
>> the dtors are virtual or not. So there seems no difference whether
>> the dtors are virtual or not. Ie. always the dtors of all parts get
> invoked.
>>
>
> In the case where a pointer isn't involved, the base's d~tor will still be
> invoked but you'll have the warning. You said "when D gets deleted". In
> the
> case you had a pointer, something i didn't include in my example, your
> base
> c~tor would not have been invoked.
>

Eh? Of course the base class destructor is invoked! Whenever a derived
class is destroyed, the destructors for any base classes *must* be invoked,
whether you're using pointers or automatic variables. Otherwise, the memory
allocated for the base class portion(s) of the object would never get
returned to the system.

This is even true if the derived class object was assigned to a base class
pointer variable. The only difference in that case is that the base class
destructor needs to be virtual, or else the *derived* class destructor will
not get invoked.

There is no way for the destruction of the derived class to *not* also
invoke the destructor of the base class (aside from invoking undefined
behavior or abnormally terminating the process).

"Howard" <> wrote in message
news:ZHire.947605$w62.229941@bgtnsc05-
>> In the case where a pointer isn't involved, the base's d~tor will still
>> be
>> invoked but you'll have the warning. You said "when D gets deleted". In
>> the
>> case you had a pointer, something i didn't include in my example, your
>> base
>> c~tor would not have been invoked.
>>
>

"c~tor"???

You *did* mean "destructor" there, didn't you? I saw the ~, and missed the
"c" in front, but you were talking about destructors, so I'm assuming that's
what you meant. If you mean the "constructor", though, my argument is the
same...the base classes' constructor(s) must also be invoked, either
explicitly or implicitly, whenever a derived class it created, regardless of
whether you're using pointers or automatic variables.

"Peter Julian" <> skrev i en meddelelse
news:B9ire.1616$...
>
> "Old Wolf" <> wrote in message
> news:...
>> Peter Julian wrote:
>> > tuvok wrote:
>> >> For a class with a virtual dtor I would have expected that only
>> >> the dtor of D would be called when D gets deleted.
>> >
>> > You are describing a non-virtual d~tor. Only D's d~tor would be
>> > invoke. Which is the crux of the arguement of why a derived class
>> > needs the base to have the virtual d~tor.
>>
>> Please don't answer questions here if you don't know what
>> you are talking about. Invoking a derived class's destructor
>> ALWAYS calls the base class destructors.
>
> He said "when D gets deleted". Please don't criticize my comments if you
> can't even read the post. Incidentally, the base destructor isn't called,
> its invoked. That means that you can't overide the base d~tor, in case you
> are wondering what the difference is.
>
> In the case a pointer to D is was involved, B's d~tor would not be
> invoked.

This is plain rubbish. If D inherits from B, B's destructor will be invoked
when deleting a D-object. And this has nothing to do with whether a
destructor is virtual or not.
>

tuvok wrote:
> Is it correct that the virtual dtor of base gets called implicitly?

Yes. Let me reiterate to make sure you understand.

When an object is created, at the level of the most
derived object all the virtual bases are constructed
(in a depth first, left to right fashion). Then the
direct bases of the classes are constructed (in the
order listed in the class definition, not the mem-initializer
list), and then each non-static data member is constructed.
Then the constructor body of the object is run.

"Peter Koch Larsen" <> wrote in message
news:Itmre.55256$...
>
> "Peter Julian" <> skrev i en meddelelse
> news:B9ire.1616$...
> >
> > "Old Wolf" <> wrote in message
> > news:...
> >> Peter Julian wrote:
> >> > tuvok wrote:
> >> >> For a class with a virtual dtor I would have expected that only
> >> >> the dtor of D would be called when D gets deleted.
> >> >
> >> > You are describing a non-virtual d~tor. Only D's d~tor would be
> >> > invoke. Which is the crux of the arguement of why a derived class
> >> > needs the base to have the virtual d~tor.
> >>
> >> Please don't answer questions here if you don't know what
> >> you are talking about. Invoking a derived class's destructor
> >> ALWAYS calls the base class destructors.
> >
> > He said "when D gets deleted". Please don't criticize my comments if you
> > can't even read the post. Incidentally, the base destructor isn't
called,
> > its invoked. That means that you can't overide the base d~tor, in case
you
> > are wondering what the difference is.
> >
> > In the case a pointer to D is was involved, B's d~tor would not be
> > invoked.
>
> This is plain rubbish. If D inherits from B, B's destructor will be
invoked
> when deleting a D-object. And this has nothing to do with whether a
> destructor is virtual or not.
>

That was a typo or rather a mistake. I meant a base B pointer to D. As in...

"Howard" <> wrote in message
news:ZHire.947605$...
>
> "Peter Julian" <> wrote in message
> news:Rfire.1619$...
> >
> > "tuvok" <> wrote in message
> > news:d8jaa5$48a$03$-online.com...
> >> "Peter Julian" wrote
> >> >
> >> > "tuvok" wrote in message
> >> >
> >> > > Is it correct that the virtual dtor of base gets called implicitly?
> >> >
> >> > Not really, the virtual d~tor of base class is invoked, not called.
> >> >
> >> > > Here's some code to demonstrate what I mean:
> >> > > Class B has a virtual destructor, so has class D which
> >> > > is derived from B. Deleting D calls the dtor of D and
> >> > > then the dtor of B.
> >> >
> >> > B's d~tor is invoked and processed before D's d~tor can complete.
> >> >
> >> > > I was thinking that this would be true only for non-virtual dtor
> >> > > case,
> >> > > but I wouldn't have expected it happen for a virtual dtor.
> >> > > For a class with a virtual dtor I would have expected that only
> >> > > the dtor of D would be called when D gets deleted.
> >> >
> >> > You are describing a non-virtual d~tor. Only D's d~tor would be
invoke.
> >>
> >> No, it's not the case. Both ~D and ~B are invoked no matter whether
> >> the dtors are virtual or not. So there seems no difference whether
> >> the dtors are virtual or not. Ie. always the dtors of all parts get
> > invoked.
> >>
> >
> > In the case where a pointer isn't involved, the base's d~tor will still
be
> > invoked but you'll have the warning. You said "when D gets deleted". In
> > the
> > case you had a pointer, something i didn't include in my example, your
> > base
> > c~tor would not have been invoked.
> >
>
> Eh? Of course the base class destructor is invoked! Whenever a derived
> class is destroyed, the destructors for any base classes *must* be
invoked,
> whether you're using pointers or automatic variables. Otherwise, the
memory
> allocated for the base class portion(s) of the object would never get
> returned to the system.
>
> This is even true if the derived class object was assigned to a base class
> pointer variable. The only difference in that case is that the base class
> destructor needs to be virtual, or else the *derived* class destructor
will
> not get invoked.
>
> There is no way for the destruction of the derived class to *not* also
> invoke the destructor of the base class (aside from invoking undefined
> behavior or abnormally terminating the process).
>
> -Howard
>

"Ron Natalie" <> wrote in message
news:42adf95a$0$26294$...
> tuvok wrote:
>> Is it correct that the virtual dtor of base gets called implicitly?
>
> Yes. Let me reiterate to make sure you understand.
>
> When an object is created, at the level of the most
> derived object all the virtual bases are constructed
> (in a depth first, left to right fashion). Then the
> direct bases of the classes are constructed (in the
> order listed in the class definition, not the mem-initializer
> list), and then each non-static data member is constructed.
> Then the constructor body of the object is run.
>
> Each subobject above recursively constructs it's subobjects
> (with the exception of avoiding duplicate construction of
> the virtual bases).
>
> There is nothing the program can do to change that ordering
> (within defined behavior).
>
>
> Objects are destructed in precisely the REVERSE ORDERING.
>
> Virtualness of the destructor, arguments to the constructor,
> etc... have no bearing on this behavior. It always happens
> in the same topological order described above.

I just wrote a program and compiled and ran to test this theory, and it
seems
that "Objects are destructed in precisely the REVERSE ORDERING" is
not always true. Here is the program:

Jim Langston wrote:
>>Each subobject above recursively constructs it's subobjects
>>(with the exception of avoiding duplicate construction of
>>the virtual bases).
>>
>>There is nothing the program can do to change that ordering
>>(within defined behavior).
>>
>>
>>Objects are destructed in precisely the REVERSE ORDERING.
>>
> I just wrote a program and compiled and ran to test this theory, and it
> seems
> that "Objects are destructed in precisely the REVERSE ORDERING" is
> not always true. Here is the program:
>
I was referring to the order of destruction of subobjects in
an object.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!