我要声明 Rect 结构:
struct{
float x,y;
float width,height;
}Rect;
并将变量 x、y union 到 'pos' 并将宽度、高度 union 到 'size' Vector2f 结构:
struct{
float x,y;
}Vector2f;
如何使用 union 来实现?
Rect rect;
//rec.x; rec.y; rect.pos; rect.pos.x; rect.pos.y;
//rect.width; rect.height; rect.size; rect.size.x; rect.size.y;
最佳答案
您正在寻找匿名 union 。语法是:
struct Rect {
union {
Vector2f pos;
struct {
float x,y;
};
};
union {
Vector2f size;
struct {
float width, height;
};
};
};
(不过我不建议这样做;我只是 KISS 并使用 vector 。)
关于c++声明Rect结构与 union ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13439610/