When should I use virtual destructors?

Is this snippet respectfully coded? I heard when using a virtual destructor should only be reccommended if your using polymorphism and or a derived class thats using virtual as well. As you can see I am using pure virtual functions. Am I doing this wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

class Math{
protected:
    float *float0;
    float *float1;
public:
    virtual float returnType()=0;
    virtual void sum()=0;
};

class Add : public Math{
public:
    Add(float, float );
    virtual ~Add();
    float returnType();
    void sum();
};

int main(){
	Add add(5.67,2.01);
	Math *math = &add;
	math->sum();
	
	return 0;
}

Add::Add(float x = 0, float y = 0){
	float0 = new float;
	float1 = new float;
	*float0 = x;
	*float1 = y;
}

Add::~Add(){
	delete float0;
	delete float1;
}

float Add::returnType(){
	return(*float0 + *float1);
}

void Add::sum(){
	std::cout<<*float0<<"  +  "<<*float1<<"  =  "<<returnType();
}
If B is a subtype of A, and you ever delete an A * that may point to an instance of B, then A::~A() needs to be virtual, or some resources may not be properly released.
For example:
1
2
3
B *b = new B;
A *a = b;
delete a; // <- Incorrect if A::~A() is not virtual. 
The most common reason for needing virtual destructors is to put polymorphic objects in a centralized container that owns their pointers:
1
2
3
4
5
6
7
std::vector<A *> v;
//...
for (auto p : v)
    p->do_it();
//...
for (auto p : v)
    delete p;
Last edited on
Can you give me an example with my source, I am just a tad bit confused.
Sure.
1
2
3
4
5
6
7
int main(){
	Add *add = new Add(5.67,2.01);
	Math *math = add;
	math->sum();
	delete math;
	return 0;
}
Following your example, line 5 is incorrect because Math::~Math() is not virtual.
Just one suggestion:
1
2
3
4
5
6
Add::Add(float x = 0, float y = 0){
	float0 = new float;
	float1 = new float;
	*float0 = x;
	*float1 = y;
}
Here, you're using pointers for no reason. Just make variables.

Oh yea, and why didn't you just make functions for these?
Ok helios, now I understand what you mean. Basically line 5 is invalid since I had setup the destructor as virtual, but if I did not set up a virtual destructor, line 5 would be valid... right?
Lumpkin, what do you mean create functions for the variables? I did make function(s), a float returnType that gets the computed addition of the pointer variables, and another function void that shows the result. Is that what you mean, or maybe somethibg else?
Basically line 5 is invalid since I had setup the destructor as virtual, but if I did not set up a virtual destructor, line 5 would be valid... right?
Line 5 is valid. A compiler won't reject it. It's incorrect because it may not properly release some resources.
But yes, making Math::~Math() virtual would make the code correct.
Ok I have a better understanding now. TY helios.
I said instead of using pointers just use normal variables.

float instead of float*

It's not necessary to use pointers.
I am just practicing with pointers and learning new tricks. I know I do not have to use it for this purpose, but I want to understand more on using them and when is the best time to use them.
Topic archived. No new replies allowed.