virtual-destructor

Does “delete” work properly with polymorphism? [duplicate]

随声附和 提交于 2019-12-22 10:44:01
问题 This question already has answers here : Virtual destructor and undefined behavior (4 answers) Closed 6 years ago . BaseClass * p = new DerivedClass(); delete p; I know the 2nd line will call the destructor of the base class if it doesn't have a virtual destructor and that of the derived class if it does but will delete properly free the memory (let's say BaseClass 's object takes up 8 bytes of space and DerivedClass 's one 12 - will it free 8 or 12 bytes) and get rid of the object in either

C++11 interface pure virtual destructor

心已入冬 提交于 2019-12-21 15:18:14
问题 UPD . There is a mark that it is a duplicate of this question. But in that question OP asks HOW to use default to define pure virtual destructor. This question is about what the difference . In C++ (latest standard if possible) what the real difference between defining pure virtual destructor with empty body implementation and just a empty body (or default)? Variant 1: class I1 { public: virtual ~I1() {} }; Variant 2.1: class I21 { public: virtual ~I21() = 0; }; I21::~I21() {} Variant 2.2:

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

心不动则不痛 提交于 2019-12-21 04:48:15
问题 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)

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

China☆狼群 提交于 2019-12-21 03:33:42
问题 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,

Why a pure virtual destructor needs an implementation

做~自己de王妃 提交于 2019-12-18 15:35:43
问题 I know the cases where pure virtual destructors are needed. I also know that If we don't provide an implementation for them it will give me a linker error. What I don't understand is why this should be the case in a code fragment as shown below: int main() { Base * p = new Derived; } Here there is no delete, so no call to destructor and so no need for its implementation(assuming it is supposed to behave like other normal functions which are declared but not defined, linker complains only when

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

与世无争的帅哥 提交于 2019-12-18 12:12:34
问题 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 non-virtual destructor? I believe compiler can decide when to use virtual destructors. So, is it a C++ design oversight, or am I missing something? 回答1: Are there any specific reasons to use non-virtual destructors? Yes, there are. Mainly, it boils down to

Is it valid to directly call a (virtual) destructor?

徘徊边缘 提交于 2019-12-18 07:55:45
问题 In this answer, Ryan directly calls the virtual destructor. I've tested the code in VS2010, and it correctly calls all destructors (tested with logging statements). Is it actually valid to do so? What are the problems, flaws or even the good points of such an approach? I can only think of it as a way to really force a reset of the actual type, even if they don't override a virtual reset function, since they atleast have to clean up in their destructors. Also, eactly what kind of side-effects

Is it valid to directly call a (virtual) destructor?

余生长醉 提交于 2019-12-18 07:53:21
问题 In this answer, Ryan directly calls the virtual destructor. I've tested the code in VS2010, and it correctly calls all destructors (tested with logging statements). Is it actually valid to do so? What are the problems, flaws or even the good points of such an approach? I can only think of it as a way to really force a reset of the actual type, even if they don't override a virtual reset function, since they atleast have to clean up in their destructors. Also, eactly what kind of side-effects

class has virtual functions and accessible non-virtual destructor

不羁岁月 提交于 2019-12-18 03:24:26
问题 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? 回答1: This happens

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

自作多情 提交于 2019-12-17 18:52:33
问题 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