Re: COMPILE!

From: JKop (NULL_at_NULL.NULL)
Date: 08/05/04


Date: Thu, 05 Aug 2004 18:07:34 GMT


>> ~AutoDestructive()
>> {
>> this->CleanUp();
>
> There's your problem - calling a virtual function from a destructor
> doesn't do what you think it does. Calling a pure virtual function
> from a destructor causes undefined behaviour (and fortunately VC seems
> to indicate this with a linker error, which is as nice as undefined
> behaviour gets).
>
> Basically, destruction goes for typical implementations:
>
> derived class destructor called
> derived members destructed in reverse order of declaration
> *vtable pointer updated to point to base class vtable*
> base class destructor called
> base class members destructed in reverse order of declaration
>
> The emphasised line is what causes the problem for you - calling a
> virtual function from a constructor or destructor calls the function
> in the base object currently being constructed or destructed, not the
> one in the most derived class.
>
> Tom

Okay, is there anything wrong with the following though? It compiles for me:

class Base
{
public:

            virtual void Monkey() = 0;

            void HelperMonkey()
            {
                        this->Monkey();
            }

            ~Base()
            {
                HelperMonkey();
            }
};

class Derived : public Base
{
public:

            virtual void Monkey()
            {

            }
};

int main()
{
            Derived checker;
}

-JKop



Relevant Pages

  • Does destructor of derived class remove virtual table?
    ... I have a small example which gives me "pure virtual function called". ... I'm calling a virtual function in the destructor of the base class, ... the pointer used is effectively pointing to a derived class, ...
    (comp.lang.cpp)
  • 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)