包含“类型"的联合成员 [英] Unions that contain a"type" member

查看:29
本文介绍了包含“类型"的联合成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于工会的问题,我仍然不明白.我已经阅读了很多它们的用途,并且在大多数情况下可以看到它们如何有用并理解它们.我已经看到它们可以提供原始的C 风格"多态性.我在几个网站上看到的例子是 SDL 的事件联合:

I have a question about something I still don't understand about unions. I've read about a lot of their uses and for the most part can see how they can be useful and understand them. I've seen that they can provide a primitive "C style" polymorphism. The example of this that I have seen on a couple websites is SDL's event union:

typedef union {
 Uint8 type;
 SDL_ActiveEvent active;
 SDL_KeyboardEvent key;
 SDL_MouseMotionEvent motion;
 SDL_MouseButtonEvent button;
 SDL_JoyAxisEvent jaxis;
 SDL_JoyBallEvent jball;
 SDL_JoyHatEvent jhat;
 SDL_JoyButtonEvent jbutton;
     SDL_ResizeEvent resize;
 SDL_ExposeEvent expose;
 SDL_QuitEvent quit;
 SDL_UserEvent user;
     SDL_SysWMEvent syswm;
} SDL_Event;

我无法理解的是如何在那里有一个类型"成员与事件类型共存?由于它们占用相同的内存区域,因此不是每次只允许存在一个吗?联合在任何时候都不会作为一种类型或事件之一存在吗?

What I cannot understand is how there can be a "type" member up there coexisting with the event types? Aren't these each only allowed to exist one at a time since they occupy the same area of memory? Wouldn't the union exist at any time as EITHER a type or one of the events?

据我了解,每个事件实际上都是一个带有类型成员的结构体,例如:

I understand that each event is actually a struct with a type member, for example:

// SDL_MouseButtonEvent

typedef struct{
     Uint8 type;
     Uint8 button;
     Uint8 state;
     Uint16 x, y;
} SDL_MouseButtonEvent;

这有什么意义?这是否以某种方式允许联合的类型成员代表联合当前的任何结构的类型?当联合的每个成员除了一个结构体并且每个结构体都包含一个成员时,这是某种奇怪的效果吗?

How does this somehow make sense? Does this somehow allow the type member of the union represent the type of whatever struct the union is currently? Is this some sort of bizarre effect that happens when every member of the union except one is a struct and each struct contains that one member?

是否可以在不知道对象是哪个结构体的情况下访问结构体成员?

Can you access struct members without knowing which struct the object is?

谢谢!

推荐答案

如果每个事件类型都有一个 Uint8 作为它的第一个数据成员,那么 type 成员union 只是为了方便.

If each of the event types has a Uint8 as its first data member, then the type member of the union is just a convenience.

联合的一般规则是您只能使用您写入的最后一个数据成员访问联合中的数据.因此,如果您上次写入 active,则下次无法从 key 读取.

The general rule with unions is that you can only access the data in the union using the last data member to which you wrote. So, if you last wrote to active, you couldn't next read from key.

此规则的一个例外是,如果联合的多个成员共享相同的前缀(如果他们的第一个数据成员相同),则您可以通过共享的联合的任何数据成员访问该前缀前缀.因此,在这里,您可以引用 active.typekey.type,无论联合的哪个数据成员处于活动状态,它都可以工作.

An exception to this rule is that if several members of a union share the same prefix (if their first data member(s) are the same), you can access that prefix via any of the data members of the union that share the prefix. So, here, you could refer to active.type or key.type, regardless of which data member of the union is active, and it would work.

SDL_Eventtype 成员只是一个方便的快捷方式,它允许您访问该 type 字段而无需将其限定为 event_object.active.typeevent_object.key.type.你可以只使用 event_object.type.

The type member of SDL_Event is just a convenient shortcut that allows you to access that type field without having to qualify it as event_object.active.type or event_object.key.type. You can just use event_object.type.

这篇关于包含“类型"的联合成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆