是否有语法将 DataItem dh.size 元素初始化为 sizeof(DataItem)?下面的代码使用构造函数并依赖编译器将事物优化为常量值。不是完美的解决方案...是否有初始化嵌套结构的语法?
struct DataHeader_t {
int sz = 0;
int type = 0;
};
struct DataItem {
DataItem() {dh.sz = sizeof(DataItem);}
DataHeader_t dh;
float data1 = 0;
float data2 = 0;
...
};
最佳答案
Not the perfect solution... Is there a syntax to initialize nested structs?
是的,该结构称为构造函数,您已经在使用它了。
关于c++ - 默认嵌套结构初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508683/