本文转载自 http://www.cnblogs.com/kaituorensheng/p/3477630.html,感谢作者分享
1. 类成员为const类型
2. 类成员为引用类型
#include <iostream> using namespace std; class A { public: A(int &v) : i(v), p(v), j(v) {} void print_val() { cout << "hello:" << i << " " << j << endl;} private: const int i; int p; int &j; }; int main(int argc ,char **argv) { int pp = 45; A b(pp); b.print_val(); }
究其因
const对象或引用只能初始化但是不能赋值。构造函数的函数体内只能做赋值而不是初始化,因此初始化const对象或引用的唯一机会是构造函数函数体之前的初始化列表中。
从无到有叫初始化,初始化(调用拷贝构造函数)创建了新对象;赋值(调用赋值操作符)没有创建新对象,而是对已有的对象赋值。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 using namespace std; 3 4 class Base 5 { 6 public: 7 Base(){cout << "Base()" << endl;} 8 Base(int a) : val(a) {cout << "Base(int a) : val(a)" << endl;} 9 private: 10 int val; 11 }; 12 13 class A 14 { 15 public: 16 A(int v) : p(v),b(v) {cout << "A(int v) : p(v), b(v)" << endl;} 17 void print_val() { cout << "hello:" << p << endl;} 18 private: 19 int p; 20 Base b; 21 }; 22 23 int main(int argc ,char **argv) 24 { 25 int pp = 45; 26 A b(pp); 27 b.print_val(); 28 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 using namespace std; 3 4 class Base 5 { 6 public: 7 Base(){cout << "Base()" << endl;} 8 Base(int a) : val(a) {cout << "Base(int a) : val(a)" << endl;} 9 private: 10 int val; 11 }; 12 13 class A : public Base 14 { 15 public: 16 A(int v) : p(v) {cout << "A(int v) : p(v), Base(v)" << endl;} 17 void print_val() { cout << "hello:" << p << endl;} 18 private: 19 int p; 20 }; 21 22 int main(int argc ,char **argv) 23 { 24 int pp = 45; 25 A b(pp); 26 b.print_val(); 27 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> using namespace std; class Base { public: //Base(){cout << "Base()" << endl;} Base(int a) : val(a) {cout << "Base(int a) : val(a)" << endl;} private: int val; }; class A : public Base { public: A(int v) : p(v),Base(v) {cout << "A(int v) : p(v), Base(v)" << endl;} void print_val() { cout << "hello:" << p << endl;} private: int p; }; int main(int argc ,char **argv) { int pp = 45; A b(pp); b.print_val(); }
3. 类成员为没有默认构造函数的类类型
#include <iostream> using namespace std; class Base { public: Base(int a) : val(a) {} private: int val; }; class A { public: A(int v) : p(v), b(v) {}//因为Base有自定义的带参构造函数,就不会产生无参构造函数,就无法产生类对象了 void print_val() { cout << "hello:" << p << endl;} private: int p;
Base b; }; int main(int argc ,char **argv) { int pp = 45; A b(pp); b.print_val(); }
原因同样是创建对象时,要初始类成员的每一个成员
4. 如果类存在继承关系,派生类必须在其初始化列表中调用基类的构造函数
#include <iostream> using namespace std; class Base { public: Base(int a) : val(a) {} private: int val; }; class A : public Base { public: A(int v) : p(v), Base(v) {} void print_val() { cout << "hello:" << p << endl;} private: int p; }; int main(int argc ,char **argv) { int pp = 45; A b(pp); b.print_val(); }