Sudipta Deb wrote:
> Hi,
> 
> I have a doubt. I know that the destructor can call virtual member function. 
> But can somebody please tell me is there any problem if destructor do the 
> same?
> 
> With regards,
> Sudipta

Seems pretty iffy.  Assuming all your destructors are virtual when you 
use base and derived classes (which they should be), the destructors are 
called from most derived to base.  If you call a virtual member function 
and the data that the function tries to access has gone away (been 
deleted), then you will probably introduce a crash bug or memory leak.

class Base
{
public:
   Base()
   {
   }

   virtual ~Base()
   {
     SomeFunc();
   }

   virtual void SomeFunc()
   {
   }
};

class Derived : public Base
{
public:
   Derived()
   {
   }

   virtual ~Derived()
   {
   }

   virtual void SomeFunc()
   {
     x = "Hi";
   }

private:
   string x;
};


Derived goes away first, then Base calls the virtual function but 'x's 
memory has already been freed.  It may still work depending on how 
'string' operates behind the scenes - that is, it may allocate memory 
and work anyway but its destructor won't be called again, leaving you 
with a memory leak.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to