Does delete work with pointers to base class?

Yes, it will work, if and only if the base class destructor is virtual, which you have done for the Base base class but not for the IFoo base class. If the base class destructor is virtual, then when you call operator delete on the base class pointer, it uses dynamic dispatch to figure out how to delete the object by looking up the derived class destructor in the virtual function table.

In your case of multiple inheritance, it will only work if the base class you’re deleting it through has a virtual destructor; it’s ok for the other base classes to not have a virtual destructor, but only if you don’t try to delete any derived objects via those other base class pointers.

Leave a Comment