Search code examples
c++derivedvirtual-destructor

Virtual destructors calling both derived and base destructors


I am having trouble understanding virtual functions. When I overload a virtual function, is the new derived function called only? Or both derived and base functions? Because I noticed that when virtual destructors are called, the base AND the derived class destructors are called.


Solution

  • destructors are special member functions in this particular regards.
    If you have a virtual base class destructor then it will properly call Base as well as derived class destructors during polymorphic deletion(calling delete on base class pointer pointinf to derived class object.).

    However, Other virtual member functions do not share the same special status as destructors. Only the function defined for the appropriate object type gets called. If you need any other function to be called you need to do so explicitly.