What is a Destructor in C++?
Learn what a destructor is in C++, when it runs, why virtual destructors matter for polymorphism, and how it powers the RAII cleanup pattern.
Expected Interview Answer
A destructor is a special member function that runs automatically when an object's lifetime ends, releasing any resources it acquired so nothing leaks.
A destructor shares the class name prefixed with a tilde (`~ClassName`), takes no arguments, cannot be overloaded, and has no return type. It runs when a stack object goes out of scope, when `delete` is called on a heap object, or when a containing object is destroyed. Destructors are the release half of RAII: constructors acquire resources like memory, file handles, or locks, and destructors free them deterministically. A base class destructor must be declared `virtual` if the class is meant to be used polymorphically, otherwise deleting a derived object through a base pointer causes undefined behavior by skipping the derived destructor. Modern C++ mostly relies on smart pointers and standard containers so custom destructors are rarely needed.
- Guarantees deterministic cleanup of acquired resources
- Prevents memory and resource leaks automatically
- Completes the RAII pattern alongside constructors
- Virtual destructors enable safe polymorphic deletion
- Runs reliably even during stack unwinding from exceptions
AI Mentor Explanation
A destructor is like the post-match ritual of removing pads and gear once a batsman is dismissed or the innings ends, ensuring nothing is left cluttering the field. If a specialist fielding gear was borrowed, it must be handed back the moment the player leaves, just as a destructor releases resources the moment an object's scope ends.
Step-by-Step Explanation
Step 1
Match the class name with a tilde
Write `~ClassName()` with no arguments and no return type.
Step 2
Runs at end of lifetime
Executes when a stack object leaves scope, `delete` is called on a heap object, or a containing object is destroyed.
Step 3
Release acquired resources
Free memory, close file handles, or unlock mutexes that the constructor acquired.
Step 4
Make base destructors virtual
Declare `virtual ~Base()` when the class may be deleted polymorphically through a base pointer.
Step 5
Rely on RAII members
Prefer members like `std::vector` or `std::unique_ptr` whose own destructors handle cleanup automatically.
What Interviewer Expects
- Knows the destructor syntax `~ClassName()` with no arguments
- Explains when a destructor runs (scope exit, `delete`, containing object destruction)
- Understands why base class destructors should be `virtual` for polymorphic types
- Connects destructors to RAII and deterministic cleanup
- Knows a class can have only one destructor (no overloading)
Common Mistakes
- Forgetting to mark a polymorphic base class destructor `virtual`
- Believing a destructor can be overloaded like a constructor
- Manually managing memory instead of relying on RAII member types
- Assuming the destructor runs at program exit rather than at scope/lifetime end
- Throwing exceptions out of a destructor
Best Answer (HR Friendly)
“A destructor is code that runs automatically when an object is no longer needed, cleaning up anything it was using, similar to returning borrowed equipment the moment you're done with it. It's how C++ programs avoid wasting memory or leaving resources open.”
Code Example
#include <iostream>
class Base {
public:
virtual ~Base() { std::cout << "Base destroyed\n"; }
};
class Derived : public Base {
public:
~Derived() override { std::cout << "Derived destroyed\n"; }
};
int main() {
Base* obj = new Derived();
delete obj;
// Output:
// Derived destroyed
// Base destroyed
return 0;
}Follow-up Questions
- Why must a polymorphic base class have a virtual destructor?
- Can a destructor throw an exception, and why is that discouraged?
- What is the destructor's role in the RAII idiom?
- In what order are member and base destructors called?
- How do smart pointers reduce the need for custom destructors?
MCQ Practice
1. What is the correct syntax for a destructor of class `Foo`?
A destructor is written as the class name prefixed with a tilde, e.g. `~Foo()`, with no arguments and no return type.
2. Why should a base class destructor be declared `virtual` if used polymorphically?
Without a virtual destructor, deleting a derived object through a base pointer only calls the base destructor, causing undefined behavior and skipped cleanup.
3. Can a class have more than one destructor?
Unlike constructors, destructors cannot be overloaded — a class has exactly one destructor, taking no arguments.
Flash Cards
What is the syntax for a destructor of class `Widget`? — `~Widget()` — no arguments, no return type.
When does a destructor run? — At scope exit for stack objects, on `delete` for heap objects, or when a containing object is destroyed.
Why make a base destructor virtual? — So deleting a derived object through a base pointer correctly calls the derived destructor too.
Can destructors be overloaded? — No — a class has exactly one destructor.