# Why do base classes need a virtual destructor?

> Why should the destructor of a polymorphic base class be virtual? What happens if it isn't?

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-virtual-destructor
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

Why should the destructor of a polymorphic base class be virtual? What happens if it isn't?

## Answer

If there is one rule you should be able to recite in your sleep for C++ interviews, it is 'make base class destructors virtual'. Let's understand why, so that it isn't just a rule you memorised.

Consider the typical polymorphic setup: 'Base\* p = new Derived();' followed later by 'delete p;'. The compiler sees only a Base pointer. If Base's destructor is not virtual, the call to the destructor is resolved statically, at compile time, against the pointer's type. So only ~Base() runs. Derived's destructor is skipped entirely, and whatever Derived owns (a heap buffer, a file handle, a lock) is never released. Even worse, the C++ standard says that deleting a derived object through a base pointer with a non-virtual destructor is undefined behaviour, not merely a leak. The program might crash, corrupt the heap, or seem to work.

If the destructor is virtual, deleting through a Base pointer performs dynamic dispatch. The most-derived destructor runs first, then destructors of each base class in reverse order of construction, all the way up. That is what you expect from object lifetime: build from the base upward, tear down from the top downward. Think of unpacking Russian nesting dolls: you must start with the outermost doll, but if you only have a handle on the inner doll's label, you need someone to tell you which outer doll to open first. The vtable plays that role.

When do you actually need it? Whenever a class is designed to be a polymorphic base, that is, whenever it has any virtual function or you expect to delete derived objects through base pointers. A handy rule: if a class has a virtual function, give it a virtual destructor. If a class is not designed for inheritance, do not make it virtual, because it adds a vptr to every object and costs a little performance. Standard library types like std::vector, std::string and std::map have non-virtual destructors, so never inherit from them for polymorphic use.

There is a second option when you want a base class that cannot be deleted through its pointer: make the destructor protected and non-virtual. Nobody can call delete on a base pointer (it fails to compile), while derived classes can still be destroyed normally. This is guideline C.35 in the C++ Core Guidelines, along with 'a base class destructor should be either public and virtual, or protected and non-virtual'.

Smart pointers help, but with caveats. std::shared_ptr<Base> p = std::make_shared<Derived>() remembers the deleter for Derived at creation time, so it works correctly even if Base's destructor isn't virtual. std::unique_ptr<Base> does not; it will call ~Base only. Do not rely on this: just make the destructor virtual.

Finally, remember pure virtual destructors need a body, and if you declare a virtual destructor as '= default', you should be aware it suppresses implicit move operations, so declare those explicitly if needed.

## Code examples

### Virtual destructor in action

```
#include <iostream>

struct Base {
    Base() { std::cout << "Base ctor\n"; }
    virtual ~Base() { std::cout << "Base dtor\n"; }   // remove 'virtual' -> UB below
};

struct Derived : Base {
    int* buf;
    Derived() : buf(new int[10]) { std::cout << "Derived ctor\n"; }
    ~Derived() override { delete[] buf; std::cout << "Derived dtor\n"; }
};

int main() {
    Base* p = new Derived;
    delete p;    // with a virtual destructor: correct full teardown
}
```

Construction goes base to derived; destruction goes derived to base. Without virtual on ~Base, this delete would be undefined behaviour.

## Key points

- Deleting a derived object through a base pointer with a non-virtual destructor is undefined behaviour.
- Virtual destructors make destruction run from the most derived class up to the base.
- Give every polymorphic base class a public virtual destructor, or a protected non-virtual one.
- Do not inherit from standard containers to use them polymorphically.
- shared_ptr captures the right deleter at creation, unique_ptr does not.

## Common follow-up questions

- Can a destructor be pure virtual? Does it need a definition?
- Why does the standard library not make std::vector's destructor virtual?
- What is the cost of making a destructor virtual?

## Interview tips

- Say 'undefined behaviour', not just 'memory leak'.
- Mention the protected non-virtual alternative from the Core Guidelines.

## Concepts covered

virtual destructor, undefined behaviour, polymorphism, resource leak

Previous: [Pure virtual functions and abstract classes](https://www.internhack.in/learn/interview/cpp-interview/cpp-pure-virtual-abstract-classes) · Next: [Object slicing](https://www.internhack.in/learn/interview/cpp-interview/cpp-object-slicing) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [Function overloading vs overriding (and name hiding)](https://www.internhack.in/learn/interview/cpp-interview/cpp-overloading-vs-overriding)
- [Operator overloading in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-operator-overloading)
- [Friend functions and inline functions](https://www.internhack.in/learn/interview/cpp-interview/cpp-friend-inline-functions)
- [Templates and template specialization](https://www.internhack.in/learn/interview/cpp-interview/cpp-templates-specialization)
- [Smart pointers: unique_ptr and shared_ptr](https://www.internhack.in/learn/interview/cpp-interview/cpp-smart-pointers)
- [weak_ptr and breaking shared_ptr cycles](https://www.internhack.in/learn/interview/cpp-interview/cpp-weak-ptr-circular-references)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
