Questions tagged [virtual-destructor]

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.

237 questions
1614
votes
17 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
  • 28,027
  • 19
  • 59
  • 89
168
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,736
  • 11
  • 33
  • 39
101
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,050
  • 8
  • 27
  • 27
78
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
  • 7,723
  • 8
  • 25
  • 25
61
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()…
51
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
  • 99,411
  • 44
  • 178
  • 249
44
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
  • 477
  • 1
  • 4
  • 9
33
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
  • 2,853
  • 4
  • 33
  • 54
30
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,501
  • 1
  • 14
  • 27
29
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,657
  • 4
  • 33
  • 56
28
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
  • 48,328
  • 33
  • 147
  • 228
24
votes
4 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
  • 10,024
  • 5
  • 43
  • 74
23
votes
1 answer

Valgrind shows memory leak in std::make_unique

I'm using Valgrind to check for memory leaks. Unfortunately I get a Leak_DefinitelyLost warning. Attached is a simplified version of my code that reproduces the error: #include #include #include #include…
21
votes
7 answers

If I change the destructor of one base class from non-virtual to virtual, what will happen?

I came across a base class whose destructor is non-virtual, although the base class have 1 virtual function fv(). This base class also has many subclasses. Many of those subclasses define its own fv(). I don't know the details of how the base and…
Yuan Wen
  • 1,343
  • 3
  • 17
  • 36
20
votes
5 answers

Are there any specific reasons to use non-virtual destructors?

As I know, any class that is designated to have subclasses should be declared with virtual destructor, so class instances can be destroyed properly when accessing them through pointers. But why it's even possible to declare such class with…
Shadows In Rain
  • 985
  • 10
  • 27
1
2 3
15 16