Search code examples
c++inheritancevirtual-destructor

Virtual destructor when using upcasting


Everyone says that a destructor should be virtual when at least one of class methods is virtual.
My questions is, isn't it correct to say that a destructor should be virtual when using upcasting ?

class A {
public:

    ~A(){
        cout << "Destructor in A" << endl;
    }
};

class B: public A
{
public:

    ~B(){
        cout << "Destructor in B" << endl;
    }
};

int main()
{
    A* a = new B;
    cout << "Exiting main" << endl;
    delete a;
}

I don't have any virtual functions in this code, but if I don't make my base destructor virtual, it will not call B destructor. And yes I know that is pointless to use ucpasting if I don't have virtual functions.

Thank you.


Solution

  • a destructor should be virtual when at least one of class methods is virtual

    This is a rule of thumb which arises from the fact that when you use virtual functions you are using runtime polymorphism and are more likely to run into situations were you need to destroy a class that may be of a derived type when all you have is a pointer to its base class subobject.

    When you destroy a derived object by using delete on a pointer to the base class a virtual destructor is necessary in the base class to avoid undefined behavior. This is the only time a virtual destructor is necessary and the guideline is intended to help avoid this situation arising.

    Herb Sutter advocated the guideline that base class destructors (i.e. destructors for classes designed to be inherited from) should be either public and virtual or protected and non-virtual. This allows the possibility that you don't intend a base class to be a point in the inheritance hierarchy which is used for deletion of derived objects and you want to enforce that this doesn't occur unintentionally.

    Of course, if you have a pure value class which isn't to be a base class, there is little you can do to stop people deriving from it anyway and then deleting derived class via pointer to base.