devxlogo

Calling a Member Function From a Destructor

Calling a Member Function From a Destructor

It’s perfectly legal to call a member function?virtual or not?from a destructor. However, you should avoid calling a member function that accesses data members that have already been destroyed. For example:

   C::~C()  {   delete p;   this->serialize(p); // undefined behavior  }

In addition, the destructor shouldn’t call a member function that throws an exception. Remember that throwing an exception from a destructor is a bad idea, because the destructor itself may have been invoked due to another exception (as part of the stack unwinding process). Trying to throw another exception will cause an immediate program termination.

devx-admin

Share the Post: