函数初始化列表
时间:2019-06-18 21:22:48
收藏:0
阅读:133
首先上一个例子
1 class Example 2 { 3 public: 4 int x; 5 int y; 6 //函数初始化列表 7 Example ():x(a),y(b){} 8 //函数内部赋值 9 Example (){this->x=a;this->y=b;}
以下参考:博客园用户starskyhu 写的很不错
以下三种情况下需要使用初始化成员列表:
一,需要初始化的数据成员是对象的情况;
二,需要初始化const修饰的类成员;
三,需要初始化引用成员数据;
拿个示例说题吧
class Example { public: Example(int _age) { age=_age; } private: const int age; };
class Example { public: Example(int _age):age(_age){} private: const int age; };
第一个类,先调用
评论(0)