Re: base class virtual destructor not resolving inherited virtual functions

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Dan Baker" <dbmail> skrev i meddelandet
news:eR2jebwwGHA.4200@xxxxxxxxxxxxxxxxxxxxxxx
"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:a47ce2tot9jk90fondglsa9o1covgm5gv0@xxxxxxxxxx
On Fri, 18 Aug 2006 13:03:06 -0600, "Dan Baker" <dbmail> wrote:

I have a heavy fog over my brain, and can't see the obvious
answer...

I have a base class "CBase" and an inherited class "CChild". I
instantiate
an object of "CChild", work with it, and then delete it. It has an
empty
destructor, which does nothing, and then calls the CBase
destructor, which
calls a virtual function which is overridden in CChild -- BUT, it
doesn't
call the overriden function (it is simply calling the base class
function).

What am I not seeing here?

In C++, virtual functions called from ctors and dtors never resolve
to
versions overridden by derived classes, because the derived parts
don't
exist at those times. C++ is unlike (and superior to) Java and C#
in this
respect.

Excellent information. I have fixed it already -- I added a
"Destroy" function that the CChild::~CChild can call to get
everything cleaned up nicely.

You shouldn't really need that. Each destructor handles the
destruction of its own part of the inheritance structure.

All of the virtual destructors are called, in reverse order (~Base
last).


Bo Persson


.



Relevant Pages

  • destructor problems
    ... i'm working on a project that derives a class from a base class (it's ... an aftermarket lib so i only have the headers to work with). ... understanding that i'll need to destroy these in the destructor for my ... i keep getting and error - Virtual function 'TMyClass::~TMyClass' ...
    (alt.comp.lang.learn.c-cpp)
  • Re: virtual destructor revisted
    ... > sure the destructor of Derived class will be invoked when a pointer of ... you want class Base to have at least one virtual function (so that you can ... functions (including destructor) and document that fact. ...
    (comp.lang.cpp)
  • Re: Tool for troubleshooting R6025 error in VC++
    ... >> Hence I don't think it is problem with virtual function call. ... > Usually pure virtual function calls occur when somebody is trying to call ... > function from constructor or destructor of a class (explicitly or ...
    (microsoft.public.vc.mfc)
  • Re: COMPILE!
    ... >typedef int HKEY; ... There's your problem - calling a virtual function from a destructor ... *vtable pointer updated to point to base class vtable* ...
    (comp.lang.cpp)
  • Re: COMPILE!
    ... > There's your problem - calling a virtual function from a destructor ... Calling a pure virtual function ... > from a destructor causes undefined behaviour (and fortunately VC seems ... > one in the most derived class. ...
    (comp.lang.cpp)