virtual-destructor

Is a virtual destructor needed for your Interface, if you always store it in a shared_ptr?

瘦欲@ 提交于 2020-08-26 19:57:09
问题 Since boost::/std::shared_ptr have the advantage of type-erasing their deleter, you can do nice things like #include <memory> typedef std::shared_ptr<void> gc_ptr; int main(){ gc_ptr p1 = new int(42); gc_ptr p2 = new float(3.14159); gc_ptr p3 = new char('o'); } And this will correctly delete all pointer thanks to the correct deleter being saved. If you ensure that every implementation of your interface always gets created with shared_ptr<Interface> (or make_shared<Interface> ), do you

Is a virtual destructor needed for your Interface, if you always store it in a shared_ptr?

时间秒杀一切 提交于 2020-08-26 19:54:32
问题 Since boost::/std::shared_ptr have the advantage of type-erasing their deleter, you can do nice things like #include <memory> typedef std::shared_ptr<void> gc_ptr; int main(){ gc_ptr p1 = new int(42); gc_ptr p2 = new float(3.14159); gc_ptr p3 = new char('o'); } And this will correctly delete all pointer thanks to the correct deleter being saved. If you ensure that every implementation of your interface always gets created with shared_ptr<Interface> (or make_shared<Interface> ), do you

What happens when delete a polymorphic object without a virtual destructor?

核能气质少年 提交于 2020-02-01 08:21:27
问题 In following example, b is a polymorphic pointer type whose static type is Base* and whose dynamic type is Derived* . struct Base { virtual void f(); }; struct Derived : Base { }; int main() { Base *b = new Derived(); // ... delete b; } What happens when b is deleted without a virtual destructor? 回答1: What happens when b is deleted without a virtual destructor? We don't know. The behavior is undefined. For most actual cases the destructor of Derived might no be invoked, but nothing is

Destructor in virtual inheritance

谁都会走 提交于 2020-01-20 08:41:27
问题 class Base{}; class D1:virtual public Base{}; class D2:virtual public Base{}; class DD:public D1,public D2{}; int main(){ Base *pBase=new DD; delete pBase; } This leads to crash, but I modify as below: class Base{ public: virtual ~Base(){}; }; class D1:virtual public Base{ public: virtual ~D1(){} }; class D2:virtual public Base{ public: virtual ~D2(){} }; class DD:public D1,public D2{ }; Then, it passes, but the default destructor should be the virtual dummy function, isn't it? 回答1: From the

Virtual destructor and undefined behavior

旧时模样 提交于 2020-01-08 17:42:30
问题 This question is different than ' When/why should I use a virtual destructor? '. struct B { virtual void foo (); ~B() {} // <--- not virtual }; struct D : B { virtual void foo (); ~D() {} }; B *p = new D; delete p; // D::~D() is not called Questions : Can this be classified as an undefined behavior (we are aware that ~D() is not going to be called for sure )? What if ~D() is empty. Will it affect the code in any way? Upon using new[] / delete[] with B* p; , the ~D() will certainly not get

What is this error message about implicitly deleted virtual destructors?

帅比萌擦擦* 提交于 2020-01-01 08:21:45
问题 I've just updated GCC from (I think) 4.5.6 to 4.6.1, under Windows, MinGW. Suddenly my NonInstantiable base class (from which you inherit with public virtual to prevent instantiation) refuses to work with the following and similar error messages: #ifndef Frigo_Lang_NonInstantiable #define Frigo_Lang_NonInstantiable namespace Frigo { namespace Lang { /** * Inherit from this class if you want to make a non-instantiable class. Most * useful for static classes. It seems every inheritance

What is this error message about implicitly deleted virtual destructors?

走远了吗. 提交于 2020-01-01 08:21:09
问题 I've just updated GCC from (I think) 4.5.6 to 4.6.1, under Windows, MinGW. Suddenly my NonInstantiable base class (from which you inherit with public virtual to prevent instantiation) refuses to work with the following and similar error messages: #ifndef Frigo_Lang_NonInstantiable #define Frigo_Lang_NonInstantiable namespace Frigo { namespace Lang { /** * Inherit from this class if you want to make a non-instantiable class. Most * useful for static classes. It seems every inheritance

Can virtual functions be inlined [duplicate]

落爺英雄遲暮 提交于 2020-01-01 02:47:14
问题 This question already has answers here : Are inline virtual functions really a non-sense? (12 answers) inline virtual function (3 answers) Closed 6 years ago . If I define a class like this: class A{ public: A(){} virtual ~A(){} virtual void func(){} }; Does it mean that that the virtual destructor and func are inlined 回答1: Whether the compiler chooses to inline a function which is defined inline is entirely up to the compiler. In general, virtual functions can only be inlined when the

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

我们两清 提交于 2019-12-31 01:50:09
问题 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