C联合-请解释
C union - please explain
据我所知,C 中的 union 一次只能保存 1 个值,而且我真的不明白 C 中的这段代码有何意义,因为 event.window不能与event.type?
同时填充
while(SDL_PollEvent(&event)) {
switch(event.type)
{
case SDL_WINDOWEVENT:
switch(event.window.event)
事件定义为:
typedef union SDL_Event
{
Uint32 type; /**< Event type, shared with all events */
SDL_CommonEvent common; /**< Common event data */
SDL_WindowEvent window; /**< Window event data */
SDL_KeyboardEvent key; /**< Keyboard event data */
SDL_TextEditingEvent edit; /**< Text editing event data */
SDL_TextInputEvent text; /**< Text input event data */
SDL_MouseMotionEvent motion; /**< Mouse motion event data */
SDL_MouseButtonEvent button; /**< Mouse button event data */
SDL_MouseWheelEvent wheel; /**< Mouse wheel event data */
SDL_JoyAxisEvent jaxis; /**< Joystick axis event data */
SDL_JoyBallEvent jball; /**< Joystick ball event data */
SDL_JoyHatEvent jhat; /**< Joystick hat event data */
SDL_JoyButtonEvent jbutton; /**< Joystick button event data */
SDL_JoyDeviceEvent jdevice; /**< Joystick device change event data */
SDL_ControllerAxisEvent caxis; /**< Game Controller axis event data */
SDL_ControllerButtonEvent cbutton; /**< Game Controller button event data */
SDL_ControllerDeviceEvent cdevice; /**< Game Controller device event data */
SDL_QuitEvent quit; /**< Quit request event data */
SDL_UserEvent user; /**< Custom event data */
SDL_SysWMEvent syswm; /**< System dependent window event data */
SDL_TouchFingerEvent tfinger; /**< Touch finger event data */
SDL_MultiGestureEvent mgesture; /**< Gesture event data */
SDL_DollarGestureEvent dgesture; /**< Gesture event data */
SDL_DropEvent drop; /**< Drag and drop event data */
/* This is necessary for ABI compatibility between Visual C++ and GCC
Visual C++ will respect the push pack pragma and use 52 bytes for
this structure, and GCC will use the alignment of the largest datatype
within the union, which is 8 bytes.
So... we'll add padding to force the size to be 56 bytes for both.
*/
Uint8 padding[56];
} SDL_Event;
union SDL_Event
的每个聚合(struct
可能)成员 SDL_CommonEvent common;
、SDL_WindowEvent window;
、SDL_KeyboardEvent key;
等...都以一些 Uint32
字段给出 type
并且那个公共 type
字段在每个联合成员中具有相同的地址和大小。
因此,虽然实际上一个联合在内存中一次只携带一个字段(换句话说,所有联合成员都有相同的地址),但每个成员都以 type
和 event.type
开头说得通;它获取 type
.
这样的成语是C中实现tagged unions的常用方式。
SDL_Event
联盟的每个成员都以相同的两个成员开始,Uint32 type
和 Uint32 timestamp
。 C 标准明确指出,如果联合当前持有一种结构类型的值,但作为另一种结构类型读取,其第一个成员与另一种结构类型匹配,则可以读取那些匹配的成员。
所有其他 SDL_X
类型都以 32 位类型开头。事实上,它们似乎都在开头包括 SDL_CommonEvent
中的字段。这是为了便于访问所有子结构的公共元素。然后,通过 event.common.x
您可以访问所有公共元素,而无需区分事件的确切类型。
据我所知,C 中的 union 一次只能保存 1 个值,而且我真的不明白 C 中的这段代码有何意义,因为 event.window不能与event.type?
同时填充while(SDL_PollEvent(&event)) {
switch(event.type)
{
case SDL_WINDOWEVENT:
switch(event.window.event)
事件定义为:
typedef union SDL_Event
{
Uint32 type; /**< Event type, shared with all events */
SDL_CommonEvent common; /**< Common event data */
SDL_WindowEvent window; /**< Window event data */
SDL_KeyboardEvent key; /**< Keyboard event data */
SDL_TextEditingEvent edit; /**< Text editing event data */
SDL_TextInputEvent text; /**< Text input event data */
SDL_MouseMotionEvent motion; /**< Mouse motion event data */
SDL_MouseButtonEvent button; /**< Mouse button event data */
SDL_MouseWheelEvent wheel; /**< Mouse wheel event data */
SDL_JoyAxisEvent jaxis; /**< Joystick axis event data */
SDL_JoyBallEvent jball; /**< Joystick ball event data */
SDL_JoyHatEvent jhat; /**< Joystick hat event data */
SDL_JoyButtonEvent jbutton; /**< Joystick button event data */
SDL_JoyDeviceEvent jdevice; /**< Joystick device change event data */
SDL_ControllerAxisEvent caxis; /**< Game Controller axis event data */
SDL_ControllerButtonEvent cbutton; /**< Game Controller button event data */
SDL_ControllerDeviceEvent cdevice; /**< Game Controller device event data */
SDL_QuitEvent quit; /**< Quit request event data */
SDL_UserEvent user; /**< Custom event data */
SDL_SysWMEvent syswm; /**< System dependent window event data */
SDL_TouchFingerEvent tfinger; /**< Touch finger event data */
SDL_MultiGestureEvent mgesture; /**< Gesture event data */
SDL_DollarGestureEvent dgesture; /**< Gesture event data */
SDL_DropEvent drop; /**< Drag and drop event data */
/* This is necessary for ABI compatibility between Visual C++ and GCC
Visual C++ will respect the push pack pragma and use 52 bytes for
this structure, and GCC will use the alignment of the largest datatype
within the union, which is 8 bytes.
So... we'll add padding to force the size to be 56 bytes for both.
*/
Uint8 padding[56];
} SDL_Event;
union SDL_Event
的每个聚合(struct
可能)成员 SDL_CommonEvent common;
、SDL_WindowEvent window;
、SDL_KeyboardEvent key;
等...都以一些 Uint32
字段给出 type
并且那个公共 type
字段在每个联合成员中具有相同的地址和大小。
因此,虽然实际上一个联合在内存中一次只携带一个字段(换句话说,所有联合成员都有相同的地址),但每个成员都以 type
和 event.type
开头说得通;它获取 type
.
这样的成语是C中实现tagged unions的常用方式。
SDL_Event
联盟的每个成员都以相同的两个成员开始,Uint32 type
和 Uint32 timestamp
。 C 标准明确指出,如果联合当前持有一种结构类型的值,但作为另一种结构类型读取,其第一个成员与另一种结构类型匹配,则可以读取那些匹配的成员。
所有其他 SDL_X
类型都以 32 位类型开头。事实上,它们似乎都在开头包括 SDL_CommonEvent
中的字段。这是为了便于访问所有子结构的公共元素。然后,通过 event.common.x
您可以访问所有公共元素,而无需区分事件的确切类型。