virtual-destructor

Should every class have a virtual destructor?

岁酱吖の 提交于 2019-11-26 22:30:30
问题 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 every class have a virtual destructor or not? Edit: Since C++11 this is no longer true, you can specify that a class is final. On the one hand giving an object a virtual destructor means it will have a vtable and therefore consume 4 (or 8 on 64 bit

Possible memory leak without a virtual destructor?

故事扮演 提交于 2019-11-26 17:23:39
问题 #include <iostream> using namespace std; class base { int a; public: base() {a =0;} }; class derv :public base { int b; public: derv() {b =1;} }; int main() { base *pb = new derv(); delete pb; } I don't have a virtual destructor in derv class, does it delete only base part of derv object?? 回答1: It might. Because base does not have a virtual destructor, your code exhibits undefined behavior. Anything might happen. It might appear to work as you expect. It might leak memory. It might cause your

Question about pure virtual destructor

谁说胖子不能爱 提交于 2019-11-26 14:46:56
问题 If we define a abstract class which has a pure virtual destructor, why do we have to give a definition of a destructor in the abstract class? 回答1: The destructor for the base class must be called when the object is destroyed, so it needs a definition. 回答2: As pointed out by Jesse, inherited destructors always get called (they are called for you by the compiler with no way to override this behavior), so it stands to reason that a virtual destructor must have an implementation. So if a pure

Why should I declare a virtual destructor for an abstract class in C++?

让人想犯罪 __ 提交于 2019-11-26 14:06:39
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. Airsource Ltd It's even more important for an interface. Any user of your class will probably hold a pointer to the interface, not a pointer to the concrete implementation. When they come to delete it, if the destructor is non-virtual, they will call the interface's destructor (or the compiler-provided default, if you didn't specify one), not the derived

Are virtual destructors inherited?

孤人 提交于 2019-11-26 11:58:13
问题 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 () {} // 2) }; Concrete questions: Is 1) and 2) the same? Is 2) automatically virtual because of its base or does it \"stop\" the virtualness? Can the derived destructor be omitted if it has nothing to do? What\'s the best practice for declaring the derived destructor? Declare

When should your destructor be virtual? [duplicate]

六眼飞鱼酱① 提交于 2019-11-26 10:23:30
问题 Possible Duplicate: When to use virtual destructors? When should your C++ object\'s destructor be virtual ? 回答1: You need virtual destructor when at least one of class methods is virtual. This is because the reason for virtual method is that you want to use polymorphism. Meaning you will call a method on the base class pointer and you want the most derived implementation - this is the whole point of polymorphism. Now if you did not have virtual destructor and through the pointer to base class

Why should I declare a virtual destructor for an abstract class in C++?

和自甴很熟 提交于 2019-11-26 03:48:27
问题 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. 回答1: It's even more important for an interface. Any user of your class will probably hold a pointer to the interface, not a pointer to the concrete implementation. When they come to delete it, if the destructor is non-virtual, they will call the

When to use virtual destructors?

浪子不回头ぞ 提交于 2019-11-25 21:35:21
问题 I have a solid understanding of most OO 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 virtual and why? 回答1: Virtual destructors are useful when you might potentially delete an instance of a derived class through a pointer to base class: class Base { // some virtual methods }; class Derived : public Base { ~Derived() { // Do some important