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.
You might also like to view...
One of the reasons why a peripheral device malfunctions can be a(n) outdated device driver.
Answer the following statement true (T) or false (F)
When creating a one-variable data table, one or more formulas that relate to the variable must be used
Indicate whether the statement is true or false
Which of the following File and Dir methods can be used to delete both files and folders?
A. remove B. delete C. kill D. del
What device acts like a wireless base station in a network, acting as a bridge between wireless and wired networks?
A. access Point B. endpoint C. WMM D. ad-hoc peer