+ 2

Benefits of virtual destructor in abstract classes?

I have an abstract class with a couple of pure virtual methods, so should i make the destructor virtual too? is there any benefit of doing that?

7th Nov 2016, 2:15 PM
Hector Sulbaran
Hector Sulbaran - avatar
3 Respostas
7th Nov 2016, 4:37 PM
மனோஜ்குமார் பழனிச்சாமி
மனோஜ்குமார் பழனிச்சாமி - avatar
+ 2
thanks
7th Nov 2016, 4:52 PM
Hector Sulbaran
Hector Sulbaran - avatar
0
Yes you should have a virtual destructor for a base class. The benefit of this is that it allows you delete a derived class object via a pointer to a base class object. Vehicle *v = new Car(); delete v; // will call derived class destructor. Failing to do this could result in a memory leak.
3rd Feb 2017, 9:08 PM
David Clark
David Clark - avatar