请描述一下在 c++ 中在此类中定义构造函数的方式?
Please describe the way the constructor is defined in this class in c++?
: 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
该参数的值。大括号为空,因为构造函数不执行任何其他操作。
它称为成员初始值设定项列表,它仅适用于构造函数。
相关文章:
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 当类成员参数具有相同的名称时,如何定义构造函数?
- C++定义构造函数使对象成为非 POD
- 请描述一下在 c++ 中在此类中定义构造函数的方式?
- 不为 emplace() 定义构造函数的解决方法
- 已定义构造函数的构造函数无法识别
- 在自定义构造函数中的堆上创建指针
- c++ 17 中结构自动定义构造函数的规则是什么?
- Python 错误:在 SWIG 生成的C++模板化代码模块中没有定义构造函数
- unique_ptr看不到派生类的自定义构造函数
- 两种类型的定义构造函数有什么区别?第一个使用":",第二个用大括号(如函数)定义它
- 为什么一个非平凡的成员需要为同一类中的匿名联合定义构造函数
- 为其成员根据模板基类存在的类定义构造函数
- 如何在派生类上具有默认行为,而不是重新定义构造函数
- 在结构中定义构造函数
- 将从基类继承的构造函数与自定义构造函数混合使用
- 如何在模板类外部定义构造函数
- C 需要使用自定义构造函数的帮助
- 可以联合包含具有用户定义构造函数的类的对象
- 具有用户定义构造函数的非聚合类的值初始化