Ask A Scientist©

Computer Science Archive


Destroying things using C++

Index Key:   COM052
Author:      benjamin heroux
Subject:     Destroying things using C++.
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?

Response #:  1 of 1
Author:      jeffrey p fergin
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?


Back to Computers Ask A Scientist Index
NEWTON Homepage Ask A Question

NEWTON is an electronic community for Science, Math, and Computer Science K-12 Educators.
Argonne National Laboratory, Division of Educational Programs, Harold Myron, Ph.D., Division Director.