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.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist