Execution order of constructors and destructors under C++ inheritance

Under inheritance, the constructor is constructed according to dependencies from strong to weak; the destructor is destructed according to the dependency chain from weak to strong.

Single inheritance

Member classes are constructed in the order of declaration and destructed in the reverse order.

The structure of a class depends on the structure of its members, and even classes are more dependent than member classes.

multiple inheritance

Spiritual classes are constructed in order and destroyed in reverse order.

The construction of a class depends on the construction of the member class, and the base class is more dependent than the member class.

In multiple inheritance, base classes are constructed in the order of declaration and destructed in the reverse order.


I recommend a Lingsheng Academy project class. I personally think the teacher taught it well. I would like to share it with you:
Lingsheng Platinum Learning Card (including infrastructure/high-performance storage/golang cloud native/audio and video/Linux kernel)
https://xxetb.xet .tech/s/VsFMs

Guess you like

Origin blog.csdn.net/qq_40135848/article/details/132906148