C + + initialization order correlation
1. Set the order of function default parameters: right-to-left
Cause: The order in which the parameters are filled in is left to right when the function is called, so the default parameters must be given from right to left
2. class data member initialization list is initialized in the order: right to left
Example:
Class Classa{public: ClassA (): m_n2 (0), m_n1 (m_n2+2) {} void Print () { cout<< "m_n1:" < < M_n1 << "m_n2:" << m_n2 <<endl; } ~classa () {}private: int m_n1; int m_n2;}; int _tmain (int argc, _tchar* argv[]) { ClassA obj; Obj. Print (); return 0;}
Output Result: M_n1:0xccccccce m_n2:0 (m_n2 initial value is 0XCCCCCCCC)
3. Initialization order of data members and constructors for a class: initializes the data member of the class before calling the constructor
Cause: When you call the constructor, the constructor may need to access the data members, so the data members need to be initialized well in advance.
4. The order in which the data members of the class and the destructor are destroyed: first call the destructor, then destroy the data members of the class
Cause: When you call a destructor, the destructor may need to access the data members, so the data members need to be refactored later.
5. Constructor call order (according to some elder sister's argument: first grandfather, then father, and son):
A. Base class constructor B. Child object constructor c. Derived class constructor
6. Sequence of calls for destructors:
A. Destructors for derived classes B. Destructors for sub-objects C. Destructors for base classes
Not finished
C + + initialization order correlation