Destroying things using C++
Date: Around 1995
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?
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:
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?
Click here to return to the Computer Science Archives
Update: June 2012