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...

Which of the following are likely to be poor choices for an identifier in a program? Why?

a. x b. RATE c. data d. _abc e. A

Computer Science & Information Technology

A network can share ________

A) keyboards B) printers only C) cell phones D) computers, printers, and files

Computer Science & Information Technology

What is nondestructive editing?

What will be an ideal response?

Computer Science & Information Technology

A set of characters with the same design and shape is a:

A. theme B. KeyTip C. font

Computer Science & Information Technology