[英]Struct variable alias
i'm trying to create an alias for a variable inside a struct like this: 我正在尝试为结构内的变量创建一个别名,如下所示:
typedef struct {
union {
Vector2 position;
float x, y;
};
union {
Vector2 size;
float width, height;
};
} RectangleF;
(note that i didn't name the unions so i dont have to write: 'variable.unionname.x' etc.) (请注意,我没有给工会命名所以我不必写:'variable.unionname.x'等)
however i get a "Initializer overrides prior initialization of this subobject" warning when i create some constants of this struct: 但是当我创建这个结构的一些常量时,我得到一个“初始化器覆盖此子对象的先前初始化”警告:
static const RectangleF RectangleFZero = {
.x = 0.0f,
.y = 0.0f, // warning
.width = 0.0f,
.height = 0.0f // warning
}
is there anything wrong doing this? 这样做有什么不对吗? and if not, how can i get rid of this warning?
如果没有,我怎么能摆脱这个警告?
Edit: Solution i use now: 编辑:我现在使用的解决方案:
typedef struct {
union {
Vector2 position;
struct { float x, y; };
};
union {
Vector2 size;
struct { float width, height; };
};
} RectangleF;
The issue is that your union is actually this: 问题是你的工会实际上是这样的:
typedef struct {
union {
Vector2 position;
float x;
float y;
};
union {
Vector2 size;
float width;
float height;
};
} RectangleF;
You could fix it by doing: 你可以通过这样做来解决它:
typedef struct {
union {
Vector2 position;
struct {
float x;
float y;
} position_;
};
union {
Vector2 size;
struct {
float width;
float height;
} size_;
};
} RectangleF;
And then do: 然后做:
static const RectangleF RectangleFZero = {
.position_.x = 0.0f,
.position_.y = 0.0f,
.size_.width = 0.0f,
.size_.height = 0.0f
};
If your compiler supports C 2011's anonymous inner structs , then you can also do this: 如果您的编译器支持C 2011的匿名内部结构 ,那么您也可以这样做:
typedef struct {
union {
Vector2 position;
struct {
float x;
float y;
};
};
union {
Vector2 size;
struct {
float width;
float height;
};
};
} RectangleF;
static const RectangleF RectangleFZero = {
.x = 0.0f,
.y = 0.0f,
.width = 0.0f,
.height = 0.0f
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.