As in
class TestA
{
TestA();
virtual ~TestA();
}
So why and when should the destructor for TestA be virtual??
1
Any class that has any virtual methods should have a virtual destructor. Otherwise, the superclass’s destructor will not be called if the object is deleted through a pointer to the child class.
In other words, if the keyword virtual
exists in the class declaration, you must at least have this:
virtual ~Class() {};
In theory, you don’t need to do this if you never delete a child object through a pointer to a base class, but in practice, it is far safer to do this at the outset regardless of how the class is used, as not doing so has no real benefit.
5
The destructor should be declared virtual if inheritance is involved and the derived class needs to be destroyed in a specific way that differs from the base class.
1