C联盟 - 请解释

时间:2015-08-31 11:32:11

标签: c sdl-2

据我所知,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;

3 个答案:

答案 0 :(得分:5)

struct的每个聚合(SDL_CommonEvent common;可能)成员SDL_WindowEvent window;SDL_KeyboardEvent key;union SDL_Event等等......都是以某些{{1}开头的提供Uint32的字段,并且该公共type字段在每个工会成员中具有相同的地址和大小。

因此,虽然联盟确实只在内存中同时携带一个字段(换句话说,所有联盟成员都具有相同的地址),但每个字符都以type开头且type有意义;它取出event.type

这种习语是C中实现tagged unions的常用方法。

答案 1 :(得分:4)

SDL_Event联盟的每个成员都以相同的两个成员Uint32 typeUint32 timestamp开头。 C标准特别指出,如果一个union当前持有一个struct类型的值,但是读作另一个结构类型,其第一个成员与另一个结构类型匹配,那么可以读取那些匹配的成员。

答案 2 :(得分:2)

所有其他SDL_X类型都以32位类型开头。实际上,它们似乎都在SDL_CommonEvent开头包含了字段。这是为了便于访问所有子结构的公共元素。然后,通过event.common.x,您可以访问所有常见元素,而无需区分事件的确切类型。