C++初始化列表的成员初始化顺序

结论:

1)与变量的声明顺序有关

2)与初始化的参数顺序有关

例子:

 1 #include <iostream>
 2 using namespace std;
 3 class DD
 4 {
 5 public:
 6     DD(int d) //DD中定义了有参构造函数
 7     {
 8         cout<<d<<endl;
 9         m_d = d;
10     }
11 private:
12     int m_d;
13 };
14 
15 class EE
16 {
17 public:
18     //构造函数的初始化列表
19     EE(int _a):d1(1),d2(2)
20     //EE(int _a):d2(1),d1(2)
21     {
22         a = _a;
23         cout<<_a<<endl;
24     }
25 
26 protected:
27 private:
28     int a;
29     DD d1;   //按照变量定义的顺序
30     DD d2;
31 
32 };
33 int main()
34 {
35     EE e1(3);
36 }
View Code
原文地址:https://www.cnblogs.com/sxmcACM/p/4473405.html