virtual-destructor

class has virtual functions and accessible non-virtual destructor

半腔热情 提交于 2019-11-29 01:32:05
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 has virtual functions and accessible non-virtual destructor class B has virtual functions and accessible non-virtual destructor I don't have any idea what this error is... Somewhere on blogs I read that it's a compiler warning. How can I fix the problem? This happens because your base class A does not have a virtual destructor. For instance, if you had this code: int main()

Virtual destructor with virtual members in C++11

♀尐吖头ヾ 提交于 2019-11-28 18:14:54
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 his second example is "Virtual destructor with virtual members" (just that). What does it mean? I know one must declare as virtual the base class destructor in order to call the right destructor if we have something like Base *b = new Derived; ... delete b; This is well explained here: When to use virtual destructors? But is it useless now in C++11

Is there any automated way to implement post-constructor and pre-destructor virtual method calls?

自作多情 提交于 2019-11-28 09:15:18
Due to the well-known issues with calling virtual methods from inside constructors and destructors, I commonly end up with classes that need a final-setup method to be called just after their constructor, and a pre-teardown method to be called just before their destructor, like this: MyObject * obj = new MyObject; obj->Initialize(); // virtual method call, required after ctor for (obj) to run properly [...] obj->AboutToDelete(); // virtual method call, required before dtor for (obj) to clean up properly delete obj; This works, but it carries with it the risk that the caller will forget to call

When should you not use virtual destructors?

谁说我不能喝 提交于 2019-11-28 03:22:03
Is there ever a good reason to not declare a virtual destructor for a class? When should you specifically avoid writing one? There is no need to use a virtual destructor when any of the below is true: No intention to derive classes from it No instantiation on the heap No intention to store in a pointer of a superclass No specific reason to avoid it unless you are really so pressed for memory. Richard Corden To answer the question explicitly, i.e. when should you not declare a virtual destructor. C++ '98/'03 Adding a virtual destructor might change your class from being POD (plain old data) *

virtual function calls in constructor and destructor [duplicate]

风格不统一 提交于 2019-11-28 02:26:31
This question already has an answer here: Calling virtual functions inside constructors 13 answers class Base { public: Base(){Foo();} ~Base(){Foo();} virtual void Foo(){std::cout<<"base";} }; class Derived: public Base { public: Derived(){Foo();} ~Derived(){Foo();} void Foo(){std::cout<<"derived";} }; //main { Derived d; } Any idea why this code prints out "base" and "derived"? I understand the advice is not to put virtual function calls inside constructor or desctructor, I just want to know why the above code would have the behaviour. Thanks During execution of the constructor of a class C ,

Virtual destructor: is it required when not dynamically allocated memory?

谁都会走 提交于 2019-11-27 19:47:15
Do we need a virtual destructor if my classes do not allocate any memory dynamically ? e.g. class A { private: int a; int b; public: A(); ~A(); }; class B: public A { private: int c; int d; public: B(); ~B(); }; In this case do we need to mark A's destructor as virtual ? The issue is not whether your classes allocate memory dynamically. It is if a user of the classes allocates a B object via an A pointer and then deletes it: A * a = new B; delete a; In this case, if there is no virtual destructor for A, the C++ Standard says that your program exhibits undefined behaviour. This is not a good

Should every class have a virtual destructor?

本小妞迷上赌 提交于 2019-11-27 16:59:49
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 machines) additional bytes per-object for the vptr . On the other hand if someone later derives from

Question about pure virtual destructor

孤街浪徒 提交于 2019-11-27 09:30:50
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? The destructor for the base class must be called when the object is destroyed, so it needs a definition. 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 virtual destructor must have an implementation, what is the difference between a pure virtual destructor and a

Are virtual destructors inherited?

坚强是说给别人听的谎言 提交于 2019-11-27 06:22:06
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 it virtual, non-virtual or omit it if possible? Yes, they are the same. The derived class not declaring

When should your destructor be virtual? [duplicate]

北慕城南 提交于 2019-11-27 03:20:58
Possible Duplicate: When to use virtual destructors? When should your C++ object's destructor be virtual ? 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 you call destructor you end up calling base class destructor. In this case you want polymorphism to work on