Linked Questions
268 questions linked to/from When to use virtual destructors?
47
votes
6
answers
26k
views
When should your destructor be virtual? [duplicate]
Possible Duplicate:
When to use virtual destructors?
When should your C++ object's destructor be virtual?
4
votes
4
answers
9k
views
Why is destructor always declared virtual [duplicate]
Possible Duplicate:
When to use virtual destructors?
Virtual destructor and undefined behavior
i am new to c++ and programming,i have observed that destructor is always declared virtual.
may i ...
4
votes
4
answers
6k
views
Is it always necessary to declare destructor as virtual, if the class contains atleast a virtual function? [duplicate]
Possible Duplicate:
When to use virtual destructors?
If all the data members of a class (which has virtual function) and it's inherited class are of non pointer type (means it can not hold any ...
5
votes
7
answers
4k
views
virtual destructor for pure abstract class [duplicate]
Based on what I found here and on other links on stackoverflow, we should always define a virtual destructor in the base class if we plan to use it polymorphically. I want to know if there is an ...
3
votes
3
answers
756
views
Why absence of a virtual destructor doesn't cause a memory leak? [duplicate]
I am playing with the code
struct A {
char a[20000];
A() { a[0] = 'A'; }
~A() {}
};
struct B : A {
char a[20000];
B() { a[0] = 'B'; }
~B() {}
};
int main() {
A *pA = new A;...
1
vote
2
answers
2k
views
Crashed when i delete a c++ object [duplicate]
Possible Duplicate:
When to use virtual destructors?
[Second dicuss]
hi,guys! You are all talking about virtual-destructor.
And also i think about the base class`s destructor.
But another test as ...
Community wiki
2
votes
2
answers
908
views
Do I need a virtual destructor if descendant classes have no non-static members or destructors? [duplicate]
I'm playing around with a class hierarchy for file descriptors, where the base class holds an int and calls close on it during destruction and child classes don't add any virtual methods or data ...
1
vote
5
answers
1k
views
why doesnt base class without virtual methods need virtual destructor? [duplicate]
i have been thinking, why only base class with virtual method needs virtual desctructor?
look at this piece of code (read the comment):
class Base{
private:
int x;
public:
Base():x(0){}
~...
-3
votes
2
answers
637
views
why do we need a virtual destructor with dynamic memory? [duplicate]
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 ...
0
votes
2
answers
3k
views
C++ unique_ptr and polymorphism [duplicate]
Maybe there is the same question but I haven't found it.
I have following code:
class MBase {
public:
~MBase() { cout << "Base destructor\n" << endl; }
};
class MF: public ...
1
vote
1
answer
916
views
why use virtual destructor in inheritance [duplicate]
Possible Duplicate:
When to use virtual destructors?
let's say i have an abstract class Animal
class Animal {
public:
Animal(const std::string &name) : _name(name)
{
}
virtual ...
3
votes
2
answers
413
views
c++ ctor and dtor don't work the same manner [duplicate]
I'm trying to figure out the tricks of class inheritance in C++ and I've built a sample project:
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
A()
{
...
-1
votes
2
answers
162
views
Not calling default constructor in C++ [duplicate]
In the following code, I traced by using debugger I fail to understand
1. why B b2() is never called but skipped.
2. Why auto_ptr calls base (A) destructor only when object created is derived (B)?
...
5
votes
1
answer
127
views
Derived from two Bases - Delete vector **strange** problem [duplicate]
I am hours trying to figurate out where is the problem, but it seems so strange.
I rewrote my problem in an easier way to understand.
When it gets to the line where it says delete, the debbug makes ...
1
vote
1
answer
109
views
Virtual destructor useful with RAII [duplicate]
My question is the following : is this following code correct :
class A {}; // no virtual destructor
class B : public A{
std::unique_ptr<int> ptr{new int(5)};
};
// in main
std::...