Menu
  • HOME
  • TAGS

exception with non virtual destructor c++

c++,exception,virtual-destructor

OK, someone already answered your first question. I'll focus on this one: if the exception destructor should be called when go out of catch scope, in this case only the base class A's d'tor will be called? The implementation will always destroy the exception object properly regardless of how it...

Division by zero works fine in virtual Destructor

c++,virtual-destructor

I guess your compiler optimizes the code and removes the useless part, including 10/i in the base class destructor. Try with this: #include <iostream> using namespace std; static int i=1; class Parent { public: virtual ~Parent(){int tmp = 10/i; cout << tmp; } }; class Child: public Parent { public:...

c++ type casting to gain access

c++,casting,virtual-destructor

For the case you mentioned above you just got lucky that the address of B's destructor is the same of A's destructor in terms of offset from the pointer. The virtual destructor is just another entry in the vftable of the object. You can call any destructor (no matter how...

When do we have to define a destructor in derived class c++

c++,destructor,virtual-destructor

in addition to R Sahu answer, note that std::queue<> destructor is not virtual, any deletion from a pointer to Base will invoke undefined behavior (for that reason, you should generally not inherit from standard containers). You should probably review your design, such as using a class member instead of inheritance,...

Compiler generated destructors and warnings in GCC and MSVC

c++,visual-c++,gcc,warnings,virtual-destructor

The class in question has no destructor (virtual or otherwise). Fun fact: All objects in C++ have a destructor. It's simply that if you don't define one, the compiler generates it, and for primitives, it's a no-op. Why is the compiler generated destructor not virtual? Because people complained that...

C++ Error when using virtual destructor [duplicate]

c++,virtual-destructor

The problem is virtual ~ISolution() = 0; the destructor is not implemented. Try virtual ~ISolution() { } instead. This is needed even when only instances of the derived class are created, because destructors are special. In a normal function, the base class implementation needs to be called explicitly, like void...

Do I need to implement my own destructors for my derived classes if they don't contain static data?

c++,polymorphism,destructor,virtual-destructor

No, virtual destructor declaration/definition of the Base class is sufficient, though making it abstract requires you give this destructor in the Derived class. If you just declare class Base { public: virtual ~Base() {} // <<< Have a definition }; you don't need to specify any destructors in inherited classes....

Destructor for Abstract Class

c#,inheritance,syntax,virtual-destructor

C# doesn't have deterministic destruction. In fact, it doesn't really have destructors, per se: it has finalizers and IDisposable. The GC will clean things up if and when it gets around to garbage collecting the object instance. All objects will [eventually] get cleaned up one way or another when the...

Calling virtual destructor from base class pointer [closed]

c++,c++11,memory-management,polymorphism,virtual-destructor

The line reinterpret_cast<Base*>(ptr)->Base::~Base(); simply needs to be reinterpret_cast<Base*>(ptr)->~Base(); instead....