Re: why virtual base dtor gets called?
"Howard" <alicebt@hotmai l.com> wrote in message
news:ZHire.9476 05$w62.229941@b gtnsc05-news.ops.worldn et.att.net...[color=blue]
>
> "Peter Julian" <p_julian@trap. trap.com> wrote in message
> news:Rfire.1619 $Qr3.338078@new s20.bellglobal. com...[color=green]
> >
> > "tuvok" <520001085531-0001@t-online.de> wrote in message
> > news:d8jaa5$48a $03$1@news.t-online.com...[color=darkred]
> >> "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[/color][/color][/color]
invoke.[color=blue][color=green][color=darkred]
> >>
> >> 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[/color]
> > invoked.[color=darkred]
> >>[/color]
> >
> > In the case where a pointer isn't involved, the base's d~tor will still[/color][/color]
be[color=blue][color=green]
> > 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.
> >[/color]
>
> Eh? Of course the base class destructor is invoked! Whenever a derived
> class is destroyed, the destructors for any base classes *must* be[/color]
invoked,[color=blue]
> whether you're using pointers or automatic variables. Otherwise, the[/color]
memory[color=blue]
> 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[/color]
will[color=blue]
> 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
>[/color]
I meant a Base * to a new D. My mistake. Its been a long day.
"Howard" <alicebt@hotmai l.com> wrote in message
news:ZHire.9476 05$w62.229941@b gtnsc05-news.ops.worldn et.att.net...[color=blue]
>
> "Peter Julian" <p_julian@trap. trap.com> wrote in message
> news:Rfire.1619 $Qr3.338078@new s20.bellglobal. com...[color=green]
> >
> > "tuvok" <520001085531-0001@t-online.de> wrote in message
> > news:d8jaa5$48a $03$1@news.t-online.com...[color=darkred]
> >> "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[/color][/color][/color]
invoke.[color=blue][color=green][color=darkred]
> >>
> >> 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[/color]
> > invoked.[color=darkred]
> >>[/color]
> >
> > In the case where a pointer isn't involved, the base's d~tor will still[/color][/color]
be[color=blue][color=green]
> > 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.
> >[/color]
>
> Eh? Of course the base class destructor is invoked! Whenever a derived
> class is destroyed, the destructors for any base classes *must* be[/color]
invoked,[color=blue]
> whether you're using pointers or automatic variables. Otherwise, the[/color]
memory[color=blue]
> 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[/color]
will[color=blue]
> 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
>[/color]
I meant a Base * to a new D. My mistake. Its been a long day.
Comment