A virtual destructor ensures a C++ object will correctly call the destructor of the most-derived class when a polymorphic object is deleted through a pointer to its base class.
Questions tagged [virtual-destructor]
247 questions
1863
votes
20 answers
When to use virtual destructors?
I have a solid understanding of most OOP theory but the one thing that confuses me a lot is virtual destructors.
I thought that the destructor always gets called no matter what and for every object in the chain.
When are you meant to make them…

Lodle
- 31,277
- 19
- 64
- 91
175
votes
7 answers
Why should I declare a virtual destructor for an abstract class in C++?
I know it is a good practice to declare virtual destructors for base classes in C++, but is it always important to declare virtual destructors even for abstract classes that function as interfaces? Please provide some reasons and examples why.

Kevin
- 7,856
- 11
- 35
- 40
108
votes
12 answers
When should you not use virtual destructors?
Is there ever a good reason to not declare a virtual destructor for a class? When should you specifically avoid writing one?

Mag Roader
- 6,850
- 8
- 32
- 27
89
votes
5 answers
Are virtual destructors inherited?
If I have a base class with a virtual destructor. Has a derived class to declare a virtual destructor too?
class base {
public:
virtual ~base () {}
};
class derived : base {
public:
virtual ~derived () {} // 1)
~derived () {} //…

cairol
- 8,573
- 9
- 27
- 25
73
votes
2 answers
Override identifier after destructor in C++11
Does the override identifier after virtual destructor declaration have any special meaning?
class Base
{
public:
virtual ~Base()
{}
virtual int Method() const
{}
};
class Derived : public Base
{
public:
virtual ~Derived()…

EnterTheNameHere Bohemian
- 1,026
- 1
- 10
- 13
59
votes
7 answers
Should every class have a virtual destructor?
Java and C# support the notion of classes that can't be used as base classes with the final and sealed keywords. In C++ however there is no good way to prevent a class from being derived from which leaves the class's author with a dilemma, should…

Motti
- 110,860
- 49
- 189
- 262
46
votes
6 answers
When should your destructor be virtual?
Possible Duplicate:
When to use virtual destructors?
When should your C++ object's destructor be virtual?

Michael Zhou
- 497
- 1
- 4
- 9
38
votes
2 answers
Does a default virtual destructor prevent compiler-generated move operations?
Inspired by the post Why does destructor disable generation of implicit move methods?, I was wondering if the same is true for the default virtual destructor, e.g.
class WidgetBase // Base class of all widgets
{
public:
virtual…

tommyk
- 3,187
- 7
- 39
- 61
32
votes
3 answers
Why are destructors not virtual by default [C++]
Why doesn't C++ make destructors virtual by default for classes that have at least one other virtual function? In this case adding a virtual destructor costs me nothing, and not having one is (almost?) always a bug. Will C++0x address this?

Jeff Linahan
- 3,775
- 5
- 37
- 56
31
votes
4 answers
Virtual destructor with virtual members in C++11
In these slides about C++11/14 standard, on slide 15, the author writes that "many classic coding rules [are] no longer applicable" in C++11. He proposes a list of three examples, and I agree with the Rule of Three and the memory management.
However…

Florian Richoux
- 1,543
- 1
- 14
- 28
30
votes
3 answers
Do I need to specify virtual on the sub-classes methods as well?
This has probably been asked before on SO, but I was unable to find a similar question.
Consider the following class hierarchy:
class BritneySpears
{
public:
virtual ~BritneySpears();
};
class Daughter1 : public BritneySpears
{
public:
…

ereOn
- 53,676
- 39
- 161
- 238
28
votes
5 answers
C++: Inheriting from std::map
I want to inherit from std::map, but as far as I know std::map hasn't any virtual destructor.
Is it therefore possible to call std::map's destructor explicitly in my destructor to ensure proper object destruction?

Sebastian Hoffmann
- 11,127
- 7
- 49
- 77
25
votes
4 answers
Virtual Default Destructors in C++
I've got a large set of inherited classes (criteria) which inherit from a base class (criterion). Here's criterion's code
class criterion
{
public:
virtual unsigned __int32 getPriorityClass() const = 0;
virtual BOOL include(fileData &file)…

Billy ONeal
- 104,103
- 58
- 317
- 552
24
votes
5 answers
A missing vtable usually means the first non-inline virtual member function has no definition
I am pretty sure this question is duplicate, but my code is different here, the following is my code. It fails with a "Undefined symbols" error, not sure whats missing.
class Parent {
public :
virtual int func () = 0;
virtual…

Aparna Chaganti
- 599
- 2
- 5
- 15
23
votes
4 answers
class has virtual functions and accessible non-virtual destructor
I have two classes:
class A {
public:
virtual void somefunction() = 0;
};
class B : public A {
public:
B();
~B();
void somefunction();
};
B::B() {}
void B::somefunction() {
// some code
}
But with g++ I get errors:
class A…

SPB
- 4,040
- 16
- 49
- 62