我如何初始化这个结构?
How do I initialize this struct?
抱歉没有具体说明,但是...
假设我有这个结构:
struct OptionData
{
Desc desc;
bool boolValue;
std::string stringValue;
int intValue;
double numberValue;
};
我是这样使用的:
OptionData isWritePatchesOptionData = {isWritePatchesDesc, {}, {}, {}, {}};
因为我有很多选择,所以我想 s.t。像这样:
<type_here> OptionDataList = {{}, {}, {}, {}};
所以我能做到:
OptionData isSeamCutOptionData = {isSeamCutDesc, OptionDataList};
但当场我无法想象 type_here 会是什么......
或者这种形式可能是不可能的……我的意思是,没有在 OptionData 结构中创建 OptionDataList 对象……但这显然是多余的……
只需提供默认初始化程序。使用
struct OptionData
{
Desc desc{};
bool boolValue{};
std::string stringValue{};
int intValue{};
double numberValue{};
};
结构中的所有内容都将被值初始化,这意味着具有构造函数的对象将被默认构造,而没有构造函数的对象将被零初始化。
这让你写
OptionData isWritePatchesOptionData = {isWritePatchesDesc}; // same as using {isWritePatchesDesc, {}, {}, {}, {}};
// and
OptionData isSeamCutOptionData = {isSeamCutDesc};
现在所有其他成员都处于 default/zero 状态。
抱歉没有具体说明,但是...
假设我有这个结构:
struct OptionData
{
Desc desc;
bool boolValue;
std::string stringValue;
int intValue;
double numberValue;
};
我是这样使用的:
OptionData isWritePatchesOptionData = {isWritePatchesDesc, {}, {}, {}, {}};
因为我有很多选择,所以我想 s.t。像这样:
<type_here> OptionDataList = {{}, {}, {}, {}};
所以我能做到:
OptionData isSeamCutOptionData = {isSeamCutDesc, OptionDataList};
但当场我无法想象 type_here 会是什么...... 或者这种形式可能是不可能的……我的意思是,没有在 OptionData 结构中创建 OptionDataList 对象……但这显然是多余的……
只需提供默认初始化程序。使用
struct OptionData
{
Desc desc{};
bool boolValue{};
std::string stringValue{};
int intValue{};
double numberValue{};
};
结构中的所有内容都将被值初始化,这意味着具有构造函数的对象将被默认构造,而没有构造函数的对象将被零初始化。
这让你写
OptionData isWritePatchesOptionData = {isWritePatchesDesc}; // same as using {isWritePatchesDesc, {}, {}, {}, {}};
// and
OptionData isSeamCutOptionData = {isSeamCutDesc};
现在所有其他成员都处于 default/zero 状态。