0

Why can we have a virtual destructor but not virtual constructor?

j0k
  • 21,914
  • 28
  • 75
  • 84
Poonam
  • 3
  • 2

1 Answers1

1

The constructor chain can be determined at compile time, because you use new ClassName() always from the most specific class possible.

However you call destructors on possibly parent classes if you use polymorphism, so you can't know at compile time where to start the chain from. You need a virtual function to always call the right one (or you'd end up with potentially uncleaned resources in the most specific classes).

Blindy
  • 55,135
  • 9
  • 81
  • 120