Several initialization sequence of C ++ classes

First to the segment of code:

 1 class A{
 2     public:
 3     int x=0;
 4     A():x(1){cout<<"A(): x="<<x<<endl;}
 5     A(int a):x(a){cout<<"A(int a): x="<<x<<endl;}
 6     A(int a,int b):x(a){
 7         cout<<"A(int a,int b): x="<<x;
 8         x=9;
 9         cout<<" "<<"x="<<x<<endl;
10     }
11 };
12 int main(){
13     A x1;
14     A x2(3);
15     A x3(5,7);
16     getchar();
17 }

Output:

 

 

 >>> >>> initialization list constructor to initialize internal initialization sequence can be seen as a member variable declarations

 

 

Also, if there are multiple variables list initialization, the initialization sequence according to the first class declared in the order, rather than what you write A (): a (1), b (2), c (3) {} on will necessarily go to a >>> b >>> c initialized!

Guess you like

Origin www.cnblogs.com/FdWzy/p/12302091.html