当第一个结构有构造函数时,如何使第二个结构工作?我得到错误:
error C2620: member 'test::teststruct::pos' of union 'test::teststruct::<unnamed-tag>' has user-defined constructor or non-trivial default constructor
编码:
struct xyz {
Uint8 x, y, z, w;
xyz(Uint8 x, Uint8 y, Uint8 z) : x(x), y(y), z(z) {}
};
struct teststruct {
union {
Uint32 value;
xyz pos; // error at this line.
};
};
我可以使用一个函数来初始化 xyz 结构,但它不会慢很多吗?更不用说:我有大量的结构,我需要创建自己的函数,其前缀为 init_xyz() 等,这并不好。有没有其他方法可以解决这个问题?