0

Is virtual destructor needed even if we have other virtual method

Hi I was under assumption that virtual destructor is needed only if there is no other virtual methods. If a class has at least one virtual method defined, destructor not marked as virtual will also work and call both derived and base destructor. This is not replicated in code below: https://www.sololearn.com/en/compiler-playground/csccofAbbQ01 Is this behavior updated recently or we must mark destructor virtual irrespective of other method being virtual or not.

13th Jan 2025, 6:14 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 2
you need virtual destructor in your base class when you do polymorphic deletion. inheritance really have big hidden costs... https://softwareengineering.stackexchange.com/questions/284561/when-not-to-use-virtual-destructors prefer composition instead of inheritance https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance
14th Jan 2025, 1:49 AM
Bob_Li
Bob_Li - avatar