Why can we have a virtual destructor but not virtual constructor?
Asked
Active
Viewed 1,670 times
0
-
When would you need one? How could you even use one? – Oct 11 '11 at 16:54
-
You can have a look to this http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors/15903538#15903538 This might help.Thanks – Tunvir Rahman Tusher May 13 '13 at 12:36
1 Answers
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
- 65,249
- 10
- 91
- 131