请描述一下在 c++ 中在此类中定义构造函数的方式?

Please describe the way the constructor is defined in this class in c++?

本文关键字:定义 构造函数 方式 c++ 描述 一下      更新时间:2023-10-16

: e(data)在下面的代码中做什么?为什么the curly brackets{ }代码中的空?类的常量成员也可以通过这种方式初始化吗? 这种定义是特定于构造函数还是可以应用于C++中的所有函数?

class binaryfile
{
private:
const entry &e;
public:
binaryfile(const entry &data) : e(data){}
ostream& write(ostream &o)
{
o<<e.b_write();
}
}
binaryfile(const entry &data) : e(data){}

定义一个构造函数,该构造函数接受一个参数并将成员变量初始化e该参数的值。大括号为空,因为构造函数不执行任何其他操作。

它称为成员初始值设定项列表,它仅适用于构造函数。