本文是对《Effective C++》的”Item 4: Make sure that objects are initialized before they’re used”的笔记和验证。
结论
- 在进入构造函数体之前,数据成员的初始化就已完成。
 - 数据成员的初始化顺序取决于声明顺序。
 
结论 1 证明
证明代码片段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
   | #include <stdio.h>
  class CA { public:     CA(const char* pName = "default")     {         printf("CA::CA(const char*) pName = %s\n", pName);     }
      CA(const CA& a)     {         printf("CA::CA(const CA&)\n");     }
      CA& operator = (const CA& a)     {         printf("CA::operator =\n");         return *this;     } };
  class CTest { public:     CTest(CA& a)     {         printf("CTest::CTest(CA)\n");         m_a = a;     }
  private:     CA m_a; };
  int main() {     CA a("special");     CTest test(a);     return 0; }
   | 
输出:
1 2 3 4
   | CA::CA(const char*) pName = special CA::CA(const char*) pName = default CTest::CTest(CA) CA::operator =
   | 
这已经能很好地证明结论 1。而进入函数体之前的数据成员的初始化如何控制呢?答案就是——成员初始化列表。
让我们来看看将CTest的构造函数改成使用成员初始化列表以后的情况:
1 2 3 4
   | CTest(CA& a): m_a(a) {     printf("CTest::CTest(CA)\n"); }
   | 
输出:
1 2 3
   | CA::CA(const char*) pName = special CA::CA(const CA&) CTest::CTest(CA)
   | 
在成员初始化列表的指定下调用了CA的复制构造函数。这两种方式的差别相当于CA a; a = b;与CA a(b);的差别,很显然使用成员初始化列表效率要更高一点。
PS: 顺便吐槽一下很多建议使用成员初始化列表而不讲为什么的老师和书,你们多讲一句能费多大劲 T.T。
结论 2 证明
证明代码片段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
   | #include <stdio.h>
  class CA { public:     CA(const char* pName = "default")     {         printf("CA::CA(const char*) pName = %s\n", pName);     }
      CA(const CA& a)     {         printf("CA::CA(const CA&)\n");     }
      CA& operator = (const CA& a)     {         printf("CA::operator =\n");         return *this;     } };
  class CB { public:     CB(const char* pName = "default")     {         printf("CB::CB(const char*) pName = %s\n", pName);     }
      CB(const CB& b)     {         printf("CB::CB(const CB&)\n");     }
      CB& operator = (const CB& b)     {         printf("CB::operator =\n");         return *this;     } };
  class CTest { public:     CTest(CA& a, CB& b): m_b(b), m_a(a)     {         printf("CTest::CTest(CA)\n");     }
  private:     CA m_a;     CB m_b; };
  int main() {     CA a("special A");     CB b("special B");     CTest test(a, b);     return 0; }
   | 
输出:
1 2 3 4 5
   | CA::CA(const char*) pName = special A CB::CB(const char*) pName = special B CA::CA(const CA&) CB::CB(const CB&) CTest::CTest(CA)
   | 
可以看出在成员初始化列表中的顺序并无作用,对成员的初始化还是以声明顺序为依据。