Give some simple recommendation for when a destructor should be declared virtual.

What will be an ideal response?

The short answer: The base class destructor should always be virtual. It costs very little. Just do it.
A somewhat longer answer is: You need a virtual base class destructor when your program applies the delete operator to a base class pointer that actually points to a derived class object. Conversely, if the base class does not have a virtual destructor, classes that inherit from the base class should not have a destructor at all.
A detailed answer is:
IF
a) Some constructor at any level of inheritance beyond the base class separately allocates resources (we have seen only memory, but there are other resources to be allocated) and
b) The program dynamically allocates an object of a class at any level beyond the base class
THEN
The base class destructor should be virtual. The consequences a non-virtual destructor are an almost certain memory leak of all memory allocated at any level beyond the base class.

Computer Science & Information Technology

You might also like to view...

If you use a binary search tree to implement an ADT dictionary, when does the efficiency of this implementation suffer?

a. when the tree loses its balance b. when the tree is too balanced c. when the data portions of the nodes are too large d. when a node accidentally has three branches

Computer Science & Information Technology

New nodes can be added to a stack and removed from the stack only at its top. For this reason a stack is referred to as a __________ data structure.

a) first-in, first-out b) linear c) last-in, first-out d) dynamic

Computer Science & Information Technology