c++声明Rect结构与 union

标签 c++

我要声明 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;
       };
   };

};

演示:http://ideone.com/JgqABu

(不过我不建议这样做;我只是 KISS 并使用 vector 。)

关于c++声明Rect结构与 union ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13439610/

相关文章:

c++ - 获取 CWnd 内具有焦点的 Controller

c++ - 使用河豚在大端和小端之间发送 dgram 消息

c++ - 调用其他类的函数

c++ - 为什么标准库min函数在下面显示错误?

c++ - 使用不同类型的 Compare 实例构造 priority_queue 实例

c++ - 错误 C2504 : base class undefined

c++ - 从大端转换为小端C++

c++ - 使用 ptime 逐日迭代

c++ - 如何将 VARIANT* 与 dynamicCall 一起使用?

c++ - MacOSX 上的 Qt Creator 或 qmake 构建库为 ".so"而不是 dylib