virtual-destructor

Why are destructors not virtual by default [C++]

浪尽此生 提交于 2019-12-03 20:34:56
问题 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? 回答1: You don't pay for what you don't need. If you never delete through base pointer, you may not want the overhead of the indirected destructor call. Perhaps you were thinking that the mere existence of the vtable is the only overhead. But each individual

Default to making classes either `final` or give them a virtual destructor?

♀尐吖头ヾ 提交于 2019-12-03 14:49:21
Classes with non-virtual destructors are a source for bugs if they are used as a base class (if a pointer or reference to the base class is used to refer to an instance of a child class). With the C++11 addition of a final class, I am wondering if it makes sense to set down the following rule: Every class must fulfil one of these two properties: be marked final (if it is not (yet) intended to be inherited from) have a virtual destructor (if it is (or is intended to) be inherited from) Probably there are cases were neither of these two options makes sense, but I guess they could be treated as

Must a c++ interface obey the rule of five?

别来无恙 提交于 2019-12-03 10:39:28
What is the correct way to declare instantiation methods when defining an interface class? Abstract base classes are required to have a virtual destructor for obvious reasons. However, the following compilation warning is then given: "'InterfaceClass' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator", which is the 'rule of five'. I understand why the 'rule of five' should be obeyed in general, but is it still applicable for an abstract base class or interface? My implimentation is then: class

No Virtual constructors but virtual destructor

ⅰ亾dé卋堺 提交于 2019-12-03 10:19:39
问题 If we dont have virtual constructors then why we have virtual destructors? Can constructors also be virtual? 回答1: There is no point in virtual constructor - you declare exactly what type is created, and it is well known in compile time. The compiler do not need [and actually cannot, since the dynamic dispatch is based on information which is created only after the object was created]. So there are no virtual constructors . Virtual destructors are important to prevent memory leaks, and monitor

order of destruction using virtual

十年热恋 提交于 2019-12-03 07:08:14
Can some one please help what the order of destruction is when I am using virtual functions. Does it start with the base class and then derived class? Since I don't see how virtual function change any objects' destruction order, I assume you're referring to the order of destruction for base classes and data members in a virtual inheritance scenario. Sub-objects are constructed base classes are constructed from most base to most derived ; multiple base classes are constructed in the order of their declaration as base classes ; virtual base classes are constructed before all others , amongst

Does a default virtual destructor prevent compiler-generated move operations?

℡╲_俬逩灬. 提交于 2019-12-03 04:21:35
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 ~WidgetBase() = default; // ... }; As the class is intended to be a base class of a widget hierarchy I have to define its destructor virtual to avoid memory leaks and undefined behavior when working with base class pointers. On the other hand I don't want to prevent the compiler from automatically generating move operations. Does a default virtual destructor prevent

Override identifier after destructor in C++11

好久不见. 提交于 2019-12-02 17:14:57
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() override {} virtual int Method() override // error: marked override, but does not override - missing const {} }; Using override identifier on virtual method is useful as check: compiler will report error when the Base virtual method is actualy not overriden. Does override on virtual destructor has any meaning/function too? John Dibling It is not override that has special

What do Clang and GCC do when `delete`ing base classes with non-virtual destructors?

纵饮孤独 提交于 2019-12-01 22:27:06
There is already a question asking about the "real-world" behavior of delete ing a pointer to a base class that lacks a virtual destructor, but the question is restricted to a very limited case (the derived class has no members with non-trivial destructors), and the accepted answer just says there's no way to know without checking the behavior of every compiler. ....but that isn't actually very helpful; knowing that every compiler might behave differently doesn't tell us anything about the behavior of any particular compiler. So, what do Clang and G++ do in this case? I would assume they would

A missing vtable usually means the first non-inline virtual member function has no definition

梦想与她 提交于 2019-12-01 14:45:04
问题 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 ~Parent(); }; class Child : public Parent { public : int data; Child (int k) { data = k; } int func() { // virtual function cout<<"Returning square of 10\n"; return 10*10; } void Display () { cout<<data<<"\n"; } ~ Child() { cout<<"Overridden Parents Destructor \n"; } };

error LNK2019 - Virtual destructor in abstract class [duplicate]

放肆的年华 提交于 2019-12-01 11:09:06
Possible Duplicate: Pure virtual destructor in C++ I have two classes: the abstract "Game" class and the derived "TestGame" class. All of the functions in TestGame are implemented separately to nothing (for the sake of getting it to compile). I am only getting one error: TestGame.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Game::~Game(void)" (??1Game@@UAE@XZ) referenced in function "public: virtual __thiscall TestGame::~TestGame(void)" (??1TestGame@@UAE@XZ) Here are my class definitions: class Game { public: virtual ~Game(void) = 0; virtual bool Initialize() = 0