A virtual destructor ensures a C++ object will correctly call the destructor of the most-derived class when a polymorphic object is deleted through a pointer to its base class.
Questions tagged [virtual-destructor]
237 questions
-1
votes
1 answer
Why can I access object that was deleted but had virtual destructor in C++?
I created simple code with 2 classes.
#include
-
#include
![](../../users/profiles/6774130.webp)
A. Dziedziczak
- 153
- 3
- 10
-1
votes
2 answers
Virtual Destructor C++
I understand that if we want to call the Destructor of our derived object which has been assigned to a pointer to base we want to make the base destructor as virtual.
However if we had something like this:
#include
using namespace…
-1
votes
3 answers
C++ Error when using virtual destructor
I have implemented an interface:
class ISolution
{
public:
virtual ~ISolution() = 0;
virtual void resultat() = 0;
};
and some derived classes from it:
class SolX : ISolution
{
private:
int member;
MyClass myOb;
public:
SolX(const…
![](../../users/profiles/1360570.webp)
thedarkside ofthemoon
- 2,041
- 4
- 26
- 44
-2
votes
2 answers
What happens when virtual destructors are declared, but have no implementation?
In C++ we can perfectly declare a function in a header file without actually implementing it. This compiles fine and usually this doesn't provide any problems, however... I am wondering if it can cause a problem when a virtual destructor is not…
![](../../users/profiles/3200088.webp)
Gio
- 2,972
- 21
- 45
-2
votes
1 answer
Uses of a Virtual destructor in C++(other than desctruction order correctness)
Every C++ programmer knows that, virtual destructor is used to ensure the proper destruction order of objects in inheritance hierarchy.
Where else "Virtual Destructors" are used/can be used in realtime scenarios?
-2
votes
2 answers
Parasoft violation:Destructor ~dest should be virtual
My company uses Parasoft to validate the correctness of our c/c++ program.
In the source code, many classes are not used as base class and they don't have virtual member functions. But they inherit from other class. Here is the sample code:
class…
![](../../users/profiles/4526762.webp)
Yuan Wen
- 1,343
- 3
- 17
- 36
-3
votes
2 answers
why do we need a virtual destructor with dynamic memory?
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?
![](../../users/profiles/2949483.webp)
user2949483
- 103
- 1
- 7
-3
votes
2 answers
C++ virtual destructor
The question is based on following class hierarchy.
Base2* d3 = new Der3();
d3->v1();
delete d3;
The output is :
Base1
Base1
Der1
Base2
Der3
v1 Base2
~Base2
And I get an exception.Why?(It only should generate memory leack)
class Base1
{
public:
…
![](../../users/profiles/466056.webp)
Yakov
- 8,699
- 25
- 100
- 182
-4
votes
1 answer
C++ virtual destructor definitions
I have three classes
class A {
// pure virtual funcs and member vars
virtual ~A();
}
class B : public A {
// some more pure virtual funcs
virtual ~B();
}
class C : public B {
// concrete implementations
~C()…
![](../../users/profiles/5339398.webp)
Madden
- 903
- 6
- 25
-4
votes
2 answers
C++ Virtual Inheritance
#include
class abc{
public:
abc *next;
protected:
int flags;
const char * name;
const char * comments;
public:
abc(const char *name, const char *comments, int…
![](../../users/profiles/743290.webp)
Vineet1982
- 7,211
- 4
- 28
- 62
-4
votes
3 answers
Same program behavior is different in g++ & MSVS 2010
I was reading this. The accepted answer of the linked question contains following code:
class Base
{
private:
virtual ~Base() = 0; /* A */
};
class Derived : protected virtual Base
{
private:
~Derived () {.......} /* B…
![](../../users/profiles/3777958.webp)
Destructor
- 13,235
- 8
- 48
- 108
-7
votes
4 answers
virtual destructor in c++
In the code below, why is the ~Derived() destructor called automatically?
#include
using namespace std;
class Base
{
public:
virtual ~Base()
{
cout << "Calling ~Base()" << endl;
}
};
class Derived: public…
![](../../users/profiles/4177619.webp)
Sajid
- 11