virtual-destructor

When should you not use virtual destructors?

蓝咒 提交于 2019-12-17 17:28:52
问题 Is there ever a good reason to not declare a virtual destructor for a class? When should you specifically avoid writing one? 回答1: 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. 回答2: To answer the question explicitly, i.e. when should you not declare a virtual destructor. C

virtual function calls in constructor and destructor [duplicate]

半世苍凉 提交于 2019-12-17 17:15:19
问题 This question already has answers here : Calling virtual functions inside constructors (13 answers) Closed 3 years ago . 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

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

北战南征 提交于 2019-12-17 15:44:58
问题 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 ? 回答1: 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

why do we need a virtual destructor with dynamic memory? [duplicate]

ぐ巨炮叔叔 提交于 2019-12-14 03:37:29
问题 This question already has answers here : When to use virtual destructors? (16 answers) Closed 5 years ago . Why do we need a virtual destructor with dynamic variables when we have inheritance? and what is the order for destructor execution in both the static/dynamic case? won't the destructor of the most derived class always execute first? 回答1: Imagine you have this: class A { public: int* x; } class B : public A { public: int *y; } main() { A *var = new B; delete var; } Please assume some

virtual destructor in c++

时光怂恿深爱的人放手 提交于 2019-12-13 11:30:37
问题 In the code below, why is the ~Derived() destructor called automatically? #include<iostream> using namespace std; class Base { public: virtual ~Base() { cout << "Calling ~Base()" << endl; } }; class Derived: public Base { private: int* m_pnArray; public: Derived(int nLength) { m_pnArray = new int[nLength]; } virtual ~Derived() { cout << "Calling ~Derived()" << endl; delete[] m_pnArray; } }; int main() { Derived *pDerived = new Derived(5); Base *pBase = pDerived; delete pBase; return 0; } 回答1:

What's this extra parameter passed into virtual destructor?

五迷三道 提交于 2019-12-12 07:44:40
问题 I have this code: class Class { public: virtual ~Class() {} }; int main() { Class* object = new Class(); delete object; } which I compile with Visual C++ 10 and get this disassembly for delete object statement: delete object; test eax,eax je wmain+23h (401041h) mov edx,dword ptr [eax] push 1 mov ecx,eax call dword ptr [edx] and this for the actual destructor: Class::`scalar deleting destructor': test byte ptr [esp+4],1 push esi mov esi,ecx mov dword ptr [esi],offset Class::`vftable' (402100h)

Would it lead to memory leak when delete base class pointer without virtual destructor?

狂风中的少年 提交于 2019-12-12 05:49:25
问题 Here is an example for explaining virtual destructor.(see http://www.geeksforgeeks.org/g-fact-37/) I modify the code based on that example, and have a question about memory leak. Suppose Base class has a variable of int num, Derived class has variable of float money. When delete base_ptr; is called, since destructor of base class is virtual, ~derived() should be called first and then ~Base() . My question is "can function delete is smart enough so that it would free the memory allocated for

Why doesn't shared_ptr have a virtual descructor? (and how can I get around this?)

∥☆過路亽.° 提交于 2019-12-11 14:43:51
问题 I wanted to make a special version of shared_ptr that would perform specific operations when it was created or destroyed, but my plans appear to be foiled by the realization that shared_ptr 's destructor is non virtual, meaning when I override it, my pointers never get cleaned up when the last instance of them are destroyed. The only alternative that comes to mind is to build in this behavior into every class that I want to use with my hypothetical custom shared_ptr , and that's not feasible

Calling Inherited IUnknown::Release() in a destructor

拥有回忆 提交于 2019-12-11 10:49:06
问题 Why does calling the inherited IUnknown::Release() function on a IWICImagingFactory object in a destructor cause a "CXX0030: Error: expression cannot be evaluated" to be shown each entry in the object's virtual function table (__vfptr)? This is in reference to an earlier question I posted but I've since realized that the problem only occurs in the destructor. The virtual function table appears valid anywhere else I have checked. However, once in the destructor all entries are shown with the

Virtual destructor and memory deallocation

北城以北 提交于 2019-12-10 22:19:56
问题 I'm not quite sure I understand virtual destructors and the concept of allocating space on the heap right. Let's look at the following example: class Base { public: int a; }; class Derived : public Base { public: int b; }; I imagine that if I do something like this Base *o = new Derived; that 8 Bytes (or whatever two integers need on the system) are allocated on the heap, which looks then something like this: ... | a | b | ... Now if I do this: delete o; How does 'delete' know, which type o