2

I don't understand how a derived class destructor calls its base class virtual destructor, but other member functions of derived class wouldn't call their base class virtual counterparts.

Is this some special treatment C++ gives to its destructors only?

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 3
    Non-virtual destructors do this too. – HolyBlackCat Oct 02 '22 at 10:35
  • Because the base class subobjects are destructed using their respective destructors. See dupe: [Why base class destructor (virtual) is called when a derived class object is deleted?](https://stackoverflow.com/questions/3261694/why-base-class-destructor-virtual-is-called-when-a-derived-class-object-is-del) – Jason Oct 02 '22 at 10:35
  • thank you. in that case, why is it strongly recommended to ad a virtual destructor if you have virtual member functions in class? – Faraz Khanafari Oct 02 '22 at 10:38
  • @FarazKhanafari See [When to use virtual destructors?](https://stackoverflow.com/questions/461203/when-to-use-virtual-destructors) for your follow up question. – Jason Oct 02 '22 at 10:39
  • 2
    Either a virtual destructor or a protected destructor. – HolyBlackCat Oct 02 '22 at 10:40

1 Answers1

0

is this some special treatment c++ gives to its destructors only?

Yes, because it is the only behavior that makes sense for a destructor. You want it to destroy all the subobjects as well. So the destructors of all members and base classes which form subobjects should be called as well.

This is not specific to virtual destructors by the way. It applies to all of them.

user17732522
  • 53,019
  • 2
  • 56
  • 105