码迷,mamicode.com
首页 > 其他好文 > 详细

函数初始化列表

时间:2019-06-18 21:22:48      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:logs   nbsp   一个   amp   stars   ref   初始化列表   col   class   

首先上一个例子

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;
};

第一个类,先调用

函数初始化列表

标签:logs   nbsp   一个   amp   stars   ref   初始化列表   col   class   

原文地址:https://www.cnblogs.com/handsometaoa/p/11007605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!