As I have read in certain forums,when derived class object is created base class members and methods are allocated space in the memory but there is no specific base class object.
Now as the derived class object goes out of scope , why derived class destructor is called first.What is the constraint of the compiler where derived class destructor cannot be called after base class destructor..?
Please correct me in case I have built a wrong understanding..Thanks in advance
When a derived class object is created, there is a specific base-class object (sub-object, really). I.e., when you create a derived object, a base class ctor is used to initialize the base class subj-object within the derived object, and only after that completes does the derived class ctor get to do its thing, initialing any members added in the derived class, etc.
Since it’s built from base to derived, it’s torn down from derived to base. When the derived dtor ends execution, there should still be a perfectly valid base object waiting for the base dtor to destroy it. If you tried to destroy the base sub-object first, however, when you ran the derived dtor, you’d no longer have a valid derived object for the derived dtor to destroy.