In C++ object oriented programming, what is the purpose of declaring a destructor as virtual, and why is a virtual destructor important when using base class pointers to derived class objects?

Difficulty: Medium

Correct Answer: A virtual destructor ensures that when a base class pointer deletes an object, the appropriate derived class destructor is called, allowing proper resource cleanup

Explanation:


Introduction / Context:
In C++ object oriented programming, destructors are special member functions that run when an object goes out of scope or is deleted, allowing resources to be released. When polymorphism is used, base class pointers often point to derived class objects. Interviewers commonly ask about virtual destructors, because forgetting to make a base class destructor virtual can cause resource leaks and undefined behaviour when deleting through a base pointer.


Given Data / Assumptions:

  • There is a base class with at least one virtual function, and one or more derived classes.
  • Objects of derived classes are created dynamically and pointers to them are stored in variables of base class pointer type.
  • Objects may hold resources such as dynamic memory, file handles, or network connections that must be released in the destructor.
  • Deletion is performed through the base class pointer using the delete operator.


Concept / Approach:
The key concept is that when deleting an object through a base class pointer, C++ will call the base class destructor unless that destructor is declared virtual. If the base destructor is virtual, the dynamic type of the object is considered and the derived class destructor is invoked first, followed by base class destructors up the hierarchy. This guarantees that all resources in the derived class are properly released. Therefore, in polymorphic base classes, the destructor must almost always be virtual to avoid resource leaks and undefined behaviour.


Step-by-Step Solution:
Step 1: Consider a base class Base with a nonvirtual destructor and a derived class Derived that allocates memory in its constructor and frees it in its destructor. Step 2: Create an object dynamically: Base *p = new Derived(); and later call delete p;. Step 3: If the Base destructor is not virtual, only Base::~Base will be called. Derived::~Derived will not be invoked, so any resources held in Derived will not be freed. Step 4: If you declare virtual ~Base(), then delete p; will perform dynamic dispatch and call Derived::~Derived first, then Base::~Base, correctly releasing all resources. Step 5: Recognise that option (a) describes this behaviour exactly and explains why virtual destructors are important.


Verification / Alternative check:
A simple way to verify is to imagine printing messages from each destructor. If the base destructor is not virtual, deleting through a base pointer will print only the base message. If the destructor is virtual, deletion prints the derived message followed by the base message. This shows that the full destruction chain is executed only when the destructor is virtual. In real programs, these destructors would release memory or close files instead of printing messages.


Why Other Options Are Wrong:
Option (b) is wrong because a virtual destructor does not prevent destruction; it controls which destructor is called. Option (c) is incorrect because declaring the destructor as virtual does not automatically make all other member functions virtual; each function must be declared virtual separately. Option (d) is wrong because the main purpose of a virtual destructor is correctness and safe polymorphic deletion, not performance optimisation.


Common Pitfalls:
A very common mistake is to define a polymorphic base class with virtual functions but forget to make the destructor virtual. This can lead to subtle bugs where derived class resources are never released. Another pitfall is to declare a virtual destructor in classes that are never intended for polymorphic use, which is unnecessary but not harmful. A good rule of thumb is: if a class is intended to be used polymorphically via base class pointers, its destructor should be virtual. Remembering this guideline is crucial for safe and robust C++ design.


Final Answer:
A virtual destructor ensures that when a base class pointer deletes an object, the appropriate derived class destructor is called, allowing proper resource cleanup.

More Questions from Technology

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion