【问题标题】:C union - please explainC联合 - 请解释
【发布时间】:2015-08-31 11:32:10
【问题描述】:

据我了解,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;

【问题讨论】:

  • 其他工会活动以Uint32 type;开头
  • union 是同一二进制数据的不同“视点”的集合。您可以分配联合的所有可能成员,但每次下一个分配都会覆盖之前存储的值。
  • C++ 标签可能是错误的。

标签: c sdl-2


【解决方案1】:

union SDL_Event 的每个聚合(可能是structs)成员SDL_CommonEvent common;SDL_WindowEvent window;SDL_KeyboardEvent key; 等...都以一些Uint32 字段开头,给出type 和常见的@ 987654329@ 字段在每个联合成员中具有相同的地址和大小。

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

这样的成语是C中实现tagged unions的常用方式。

【讨论】:

    【解决方案2】:

    SDL_Event 联合的每个成员都以相同的两个成员开头,Uint32 typeUint32 timestamp。 C 标准明确规定,如果联合当前持有一个结构类型的值,但读取为另一个结构类型,其第一个成员与另一个结构类型匹配,则可以读取那些匹配的成员。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2018-07-25
        • 1970-01-01
        • 2018-03-03
        • 2010-10-22
        • 1970-01-01
        • 2017-09-05
        • 2013-03-15
        • 2013-03-19
        • 2020-07-20
        相关资源
        最近更新 更多