C ++ - 有关包含&QUOT工会问题;类型"会员 [英] C++ - question about unions that contain a"type" member

查看:133
本文介绍了C ++ - 有关包含&QUOT工会问题;类型"会员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于我的东西还是不理解工会的问题。我读过了很多自己的用途和大部分能看到他们如何可能是有用的,理解他们。我已经看到了它们能够提供原始的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;

请问这个在某种程度上有意义吗?这是否在某种程度上允许重新present的任何结构的结合是目前类型的工会会员类型?这是某种离奇的效果当这种情况发生,除了一个联盟的每一个成员是一个结构,每个结构包含一个成员?

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 作为其第一个数据成员,那么键入联盟的成员只是一个方便。

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.

与工会的一般规则是,你可以使用你写了最后的数据成员只能访问在工会中的数据。所以,如果你上次写的有效,你不能从旁边读

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.

此规则的一个例外是,如果工会份额的几名成员在同一preFIX(如果他们的第一个数据成员(们)都一样),你可以通过任何数据访问preFIX联盟成员共享preFIX。所以,在这里,你可以参考 active.type key.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_Event 键入成员仅仅是一个方便的快捷方式,它允许您访问 event_object.active.type 或 event_object.key.type 键入字段$ C>。你可以只用 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.

这篇关于C ++ - 有关包含&QUOT工会问题;类型"会员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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