如何通过构造函数初始化所有对象
How do you go about initializing all the objects by the constructor?
如何通过构造函数初始化所有对象?
假设在Location类中有4个Set对象。如何初始化构造函数中的4个set对象以设置为默认对象?我试图首先创建已经初始化的对象:
设置nyX(nyXv);然后尝试将其放入构造函数中,但这不会起作用。
class Location
{
public:
vector<int> nyXv = { 0, 1, 2, 3, 4, 5};
vector<int> nyYv = { 0, 1, 2, 3, 4, 5 };
vector<int> sfXv = { 6, 7, 8, 9, 10 };
vector<int> sfYv = { 6, 7, 8, 9, 10 };
Set nyX;
Set nyY;
Set sfX;
Set sfY;
Location();
~Location();
};
class Set
{
public:
Set(vector<int> &);
};
您应该使用一个初始化列表:
Location::Location()
: nyX(nyXv)
{}
相关文章:
- 用C++中的CPerson(类)类型的对象初始化STL矢量
- 对象初始化中是否允许指向此成员的指针?
- 对象初始化后在C++中显示 char 数组时的异常行为
- 为什么两种不同的对象初始化方式给出不同的输出
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- afxmem.cpp中的对象初始化差异
- 删除通过取消引用新对象初始化的对象
- 如何使用sregex_token_iterator对象初始化向量
- 关于默认构造函数,对象初始化/使用C++ OOP
- C++ 基元类型初始化与对象初始化
- 哪个函数负责C++全局范围内的类对象初始化?
- C++中构造函数的对象初始化出现问题
- 如何修复模板 BST 类的对象初始化
- 无法在 QML/C++ 中使用绑定对象初始化 UI
- 为什么参数可以在对象初始化时通过赋值运算符传递给构造函数?
- 类对象初始化的二维向量
- GCC:当层次结构中存在虚拟继承时,C++11 内联对象初始化(使用 "this")不起作用
- 对象初始化
- 类对象初始化
- 使用从另一个类继承的类的对象初始化成员对象