Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

When to use virtual destructors in C++ ?

I have an understanding of most OOP theory but the one thing that confuses me is virtual destructors.

I thought that the destructor always gets called no matter what and for every object in the chain.

When are you meant to make them virtual and why?
by

2 Answers

Bharatgxwzm
Virtual destructors are required when you will be utilizing these items polymorphically. It's sufficient for the base class destructor to be virtual; the determined destructors will be certainly virtual also.

For your situation, it doesn't seem as though you will utilize the discoursed polymorphically, so maybe you needn't bother with a virtual destructor by any stretch of the imagination.
MounikaDasa
You need virtual functions if you have an object whose type is known at runtime but not known at compile time.

You may destroy an object without knowing its exact type, by calling the virtual destructor on its pointer. Here is where we use virtual destructors in C++

Login / Signup to Answer the Question.