构造函数后跟冒号
来源:互联网 发布:淘宝上卖的沙发怎么样 编辑:程序博客网 时间:2024/06/07 10:16
1、Problem
class GeoNeighborsTimer : public TimerCallback {
public:
GeoNeighborsTimer(GeoRoutingFilter *agent) : agent_(agent) {};
~GeoNeighborsTimer() {};
int expire();
GeoRoutingFilter *agent_;
};
只知道在类后加冒号后跟继承的父类,可这构造函数后跟一个冒号,后边又有类的成员,又有构造函数的参数,这是什么意思呢?
2、Anwser
构造函数后跟冒号
表示先对冒号后的类成员(参数中的哪个)进行初始化,然后做为冒号前类的成员。
class Student {
public:
Student() {}
Student( const string& nm, int sc = 0 )
: name( nm ), score( sc ) {}
//这个跟name=nm ;score=sc;语句效果一样
private:
string name;
int score;
};
关于什么时候得这么用?是不是必须得这么用?和是不是只能用在构造函数?待考证 。。。。
from: c++ primer
Constructors构造函数
When we create an object of a class type, the compiler automatically uses a constructor (, p. ) to initialize the object. A constructor is a special member function that has the same name as the class. Its purpose is to ensure that each data member is set to sensible initial values.
创建一个类类型的对象时,编译器会自动使用一个构造函数()来初始化该对象。构造函数是一个特殊的、与类同名的成员函数,用于给每个数据成员设置适当的初始值。
A constructor generally should use a (, p. ), to initialize the data members of the object:
构造函数一般就使用一个构造函数初始化列表(),来初始化对象的数据成员:
// default constructor needed to initialize members of built-in typeSales_item(): units_sold(0), revenue(0.0) { }The constructor initializer list is a list of member names and parenthesized initial values. It follows the constructor's parameter list and begins with a colon.
- 构造函数后跟冒号
- 构造函数后跟冒号
- C++ 构造函数 冒号
- C++构造函数后面的冒号
- C++ 构造函数后加冒号
- c++ 构造函数后面加冒号
- 冒号初始化以及构造函数内赋值
- C++构造函数后面的冒号
- C++构造函数后面的冒号
- C++构造函数后面的冒号
- 函数声明后跟throw()
- C++类成员冒号初始化以及构造函数内赋值
- C++构造函数中的初始化:冒号的使用
- C++类成员冒号初始化以及构造函数内赋值
- C++类成员冒号初始化以及构造函数内赋值
- C++类成员冒号初始化以及构造函数内赋值
- C++ 构造函数后面的冒号的作用
- [C++]类成员冒号初始化以及构造函数内赋值
- Android 支持不同屏幕的方法
- SGU 105 Div 3 找规律
- 为Eclipse安装安卓开发插件ADT
- 16,Xcode 4.4中LLVM compiler 4.0带来的Objective-C新语法特性
- 发送邮件
- 构造函数后跟冒号
- ORACLE中的支持正则表达式的函数
- linux 定时器
- Chrome浏览器调用摄像头拍照
- DICT 和V$PARAMETER_VALID_VALUES
- django unexpected keyword argument 'use_decimal' 问题
- web 字符串处理
- 剑指Offer之二进制中1的个数
- 【Android】菜单功能的实现:使用XML