Summary

A virtual destructor is a destructor marked virtual so that deleting an object through a base pointer runs the destructor of its real type. If the base destructor is not virtual and you delete a derived object through a base pointer, the behavior is undefined by the standard. The run shown here (g++ 14.2, -std=c++17, one run that printed only ~Base) is an observation, not a guarantee.

What is a virtual destructor?

One-line answer: It is a destructor declared virtual so that delete through a base pointer calls the destructor of the actual (dynamic) type.

A virtual destructor is a destructor declared with virtual. When a polymorphic class is used through Base* pointing at a derived object, and you delete it, typical implementations dispatch through the vtable so the destructor of the real type (Derived) runs first. Then the base class destructors run in reverse order of construction.

The default rule is: a base class meant to be used polymorphically gets a public virtual destructor. Why releasing resources belongs in the destructor is covered in C++ RAII, so I will not repeat it here.

What happens if you leave out virtual?

One-line answer: The behavior is undefined by the standard. In one run in this environment, ~Derived was not printed.

Here is the full vdtor_bad.cpp, where the destructor has no virtual.

#include <cstdio>
struct Base {
    ~Base() { std::puts("~Base"); }          // virtual 없음
    virtual void hello() { std::puts("Base::hello"); }
};
struct Derived : Base {
    ~Derived() { std::puts("~Derived"); }
    void hello() override { std::puts("Derived::hello"); }
};
int main() {
    Base* p = new Derived;
    p->hello();
    delete p;   // 부모 포인터로 delete
}

Compiling produces only a warning, not an error (exit code 0). This is the output as printed. (The comments inside the code are in Korean: “no virtual” and “delete through the parent pointer”.)

$ g++ -std=c++17 -Wall vdtor_bad.cpp -o vdtor_bad
vdtor_bad.cpp: In function ‘int main()’:
vdtor_bad.cpp:13:5: warning: deleting object of polymorphic class type ‘Base’ which has non-virtual destructor might cause undefined behavior [-Wdelete-non-virtual-dtor]
   13 |     delete p;   // 부모 포인터로 delete
      |     ^~~~~~~~

Here is how to read the warning.

  • It points at delete p on line 13.
  • It names the polymorphic class Base, which has a non-virtual destructor.
  • The flag is -Wdelete-non-virtual-dtor, which was enabled here by -Wall.

Run output (exit code 0):

$ ./vdtor_bad
Derived::hello
~Base

In this environment (g++ 14.2, -std=c++17), one run printed only ~Base. Because the behavior is undefined by the standard, other compilers, options, or code can give different results, and no result is guaranteed. If ~Derived does not run, resources held by the derived part may not be released. Also, running this program under ASan reported nothing, so do not rely on tools to catch it.

vdtor_good.cpp differs only on line 3. (The comment says “virtual added”.)

    virtual ~Base() { std::puts("~Base"); }  // virtual 추가

g++ -std=c++17 -Wall vdtor_good.cpp -o vdtor_good printed nothing and exited with 0. Run output (exit code 0):

$ ./vdtor_good
Derived::hello
~Derived
~Base
  • Without virtual, only ~Base was printed (one observation here, not guaranteed)
  • With virtual, ~Derived and then ~Base were printed

Destructor call order: without vs with virtual

When is virtual not needed?

One-line answer: When the class is never deleted through a base pointer, or when the design blocks that path.

A class that is not meant for inheritance can be marked final, so it never serves as a base. For a base class that should not be deleted through a base pointer, you can make the destructor protected and non-virtual. Then code that deletes through Base* does not compile.

C++ Core Guidelines C.35 puts it this way: “A base class destructor should be either public and virtual, or protected and non-virtual”. In other words, pick one of the two and avoid a public non-virtual destructor in a base class.

Is it fine if I use smart pointers?

One-line answer: unique_ptr<Base> uses default_delete<Base>, which is a delete on a Base*, so the same problem remains.

Even if a unique_ptr<Base> holds a Derived object, destruction calls delete on a Base*, so without a virtual base destructor you have the same problem as above. A shared_ptr may remember a deleter for the type it was created with, but do not rely on that; the rule is to make the destructor virtual. For how to choose between the two pointers, see C++ unique_ptr and shared_ptr, how to choose in interviews (Korean).

How should you answer this in an interview?

One-line answer: Say: “I make the destructor virtual so that polymorphic delete calls the destructor of the real type. Without it, the behavior is undefined.”

Why make the destructor virtual?

So that deleting through a base pointer calls the destructor of the real type. Without virtual, the behavior is undefined by the standard; in this post’s environment only ~Base was printed, but that result is not guaranteed.

Should every class have a virtual destructor?

No. For a class that is not used polymorphically, virtual only adds the cost of a vtable pointer. Put it on base classes that will be deleted through a base pointer.

Can a constructor be virtual?

No. A constructor cannot be declared virtual.

One-line answer: These four posts help if you want more on destructors and object lifetime.

What are the sources?

One-line answer: Standard behavior comes from cppreference and the C++ Core Guidelines; the run results are my own measurements.