Reputation: 29
Why order of constructors called in inheritance is reverse than calls to destructors ? Any specific reason ?
Upvotes: 0
Views: 89
Reputation: 25388
Yes there is. The order for constructors is base
, derived
. This ensures that the constructor for derived
can rely on base
being properly initialised while the derived
constructor runs.
The order for destructors is derived
, base
. This ensures that derived
can rely on base
still being valid while the derived
destructor runs.
Upvotes: 4