Overview of Virtual Functions - Virtual Deconstructor
(Page 4 of 5 )
You've probably heard from more experienced programmers that all deconstructions should be made virtual. The day arrived to learn why this is so crucial. Until now, you may have used only non-virtual destructors.
If a derived-class object with a non-virtual deconstructor is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the C++ standard specifies that the behavior is undefined.
The derived class is a base object, but also something more. Destroying only the base makes no sense and can have different results depending on the machine on which it runs. Most probably, you will wake up with a few memory leaks. Wasting memory is against the basic principles of good software engineering.
The solution is to append the virtual keyword in the front of the destructor. Do this as high as you can in the class hierarchy, preferably in the main root (base) class. As the rule of virtual functions explains, all destructors in the derived class will also be virtual. It is not necessary to make its derived deconstructors virtual, but this will improve readability; even someone from the outside will be able maintain the code with less effort.
When you delete explicitly with the delete keyword an object using a base pointer to the object, instead of the base pointer's function the derived classes, a deconstructor is responsible for clearing/deleting the object. The constructor will begin from the bottom of the hierarchy, destroy one object, and step further up until it destroys the main base (root) object.
Here is our class at work. As you can see, we can call without fear the getPrice() function, without implementing it in the base, and not worry that it does not exist:
BananaContainer banana(1, 2, 50);
CarContainer car(2, "Lamborghini Murcielago", 313000);
EmptyContainer empty(3);
std::vector<BaseContainer*> allContainer;
allContainer.push_back(&banana);
allContainer.push_back(&car);
allContainer.push_back(&empty);
std::vector<BaseContainer*>::iterator it, end;
for (it = allContainer.begin(), end = allContainer.end();
it != end; ++it)
{
cout << (*it)->getPrice() << endl;
}
100
313000
0
Press any key to continue . . .
You probably remember the run-time type defining technique we learned in the past article under the name of down casting. Although this is a powerful method, be careful to avoid the cast for each one, and thus lose yourself inside a switch like logic. Today I am going to present another scheme to find out the type of an object at run time. The keyword used is typeid().
Next: Virtual Deconstructor continued >>
More C++ Articles
More By Gabor Bernat