Department of Energy Argonne National Laboratory Office of Science NEWTON's Homepage NEWTON's Homepage
NEWTON, Ask A Scientist!
NEWTON Home Page NEWTON Teachers Visit Our Archives Ask A Question How To Ask A Question Question of the Week Our Expert Scientists Volunteer at NEWTON! Frequently Asked Questions Referencing NEWTON About NEWTON About Ask A Scientist Education At Argonne Destroying things using C++
Name: Ben
Status: N/A
Age: N/A
Location: N/A
Country: N/A
Date: Around 1995


Question:
Is there a way to override the base class destructor's execution in the derived class' destructor in case the derived class needs to handle a member's deletion differently?



Replies:
Ok, here is something that you can try:

In the base class, make the destructor virtual. For example, instead of:
~base_class();  do this: virtual ~base_class();
Now, within the derived class, you should be able to do this:
~base_class();  
If you do not "overload" ~base_class() within the derived class, then the original destructor will be invoked when an instance of the class goes out of scope. Otherwise, the code that you have within the derived class will be used for ~base_class() when an instance goes out of scope. Understand all of that?

J Fergin


Click here to return to the Computer Science Archives

NEWTON is an electronic community for Science, Math, and Computer Science K-12 Educators, sponsored and operated by Argonne National Laboratory's Educational Programs, Andrew Skipor, Ph.D., Head of Educational Programs.

For assistance with NEWTON contact a System Operator (help@newton.dep.anl.gov), or at Argonne's Educational Programs

NEWTON AND ASK A SCIENTIST
Educational Programs
Building 360
9700 S. Cass Ave.
Argonne, Illinois
60439-4845, USA
Update: June 2012
Weclome To Newton

Argonne National Laboratory