C++ 联合、结构、成员类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3071806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:59:28  来源:igfitidea点击:

C++ Union, Struct, Member type

c++structunions

提问by Nick Heiner

If I have a class:

如果我有一堂课:

class Odp
{
    int i;
    int b;
    union
    {
         long f;
         struct
         {
               WCHAR* pwszFoo;
               HRESULT hr;
         };
    };

}

Union means that, of all values listed, it can only take on one of those values at a time? How does that work in terms of accessing these variables? How would I access hrdirectly? If I set hr, what happens if I try to access f?

联合意味着,在列出的所有值中,它一次只能采用其中一个值?在访问这些变量方面它是如何工作的?我如何hr直接访问?如果我设置了hr,如果我尝试访问会发生什么f

回答by

This is a very fraught area in the C++ standard - basically a union instance, per the standard can only be treated at any one time as if it contained one "active" member - the last one written to it. So:

这是 C++ 标准中一个非常令人担忧的领域——基本上是一个联合实例,每个标准只能在任何时候被视为包含一个“活动”成员——最后一个写入它的成员。所以:

union U {
   int a;
   char c;
};

then:

然后:

U u;
u.a = 1;
int n = u.a;
u.c = 2;
char c = u.c;

is OK, but:

可以,但是:

U u;
u.a = 1;
char c = u.c;

is not. However, there are vast volumes of existing code that say that both are OK. and in neither, or any, case will an exception be thrown for an "invalid" access. The C++ language uses exceptions exceptionally (!) sparingly.

不是。但是,有大量现有代码表明两者都可以。并且在任何一种情况下都不会因“无效”访问而引发异常。C++ 语言非常谨慎地使用异常 (!)。

Basically, if you find yourself using unions in your C++ code to deal with anything but C libraries, something is wrong.

基本上,如果您发现自己在 C++ 代码中使用联合来处理除 C 库之外的任何内容,那么一定有问题。

回答by AnT

Every time you set (write to) a member of a union, you essentially make it "active". You are only allowed to read the currently active member of the union. It means that it is your responsibility to remember somehow which member is active at each moment in time.

每次您设置(写入)一个联合成员时,您基本上都将其设为“活动”。您只能阅读工会当前的活跃成员。这意味着您有责任及时记住哪个成员处于活动状态。

Attempting to access the inactive member of a union leads to undefined behavior.

尝试访问联合的非活动成员会导致未定义的行为。

Keep in mind also that your code is not valid C++. There's no such thing as "anonymous struct" in C++. Your struct member has to have a name. If your compiler accepts it, it is just a non-standard extension supported by your specific compiler.

还要记住,您的代码不是有效的 C++。C++ 中没有“匿名结构”这样的东西。你的结构成员必须有一个名字。如果您的编译器接受它,则它只是您的特定编译器支持的非标准扩展。

回答by Justin Ethier

Right, with a unionthe same memory locations will be used to represent a single one of the members at any given time. So if you have an instance of the union and set the value of hr, you will get garbage if you then try to read the value of f.

没错,union在任何给定时间,具有相同内存位置的成员都将用于表示单个成员。因此,如果您有一个联合实例并设置了 的值hr,那么如果您尝试读取 的值,则会得到垃圾f

Try using the following to access hr:

尝试使用以下方法访问hr

union a;
a.hr = NULL;

回答by John Weldon

It just means you can access the same memory as either the long, or the struct.

这只是意味着您可以访问与 long 或 struct 相同的内存。

To access hr:

访问hr

Odp o1;
o1.hr;

Interesting link: http://www.cplusplus.com/forum/general/18816/

有趣的链接:http: //www.cplusplus.com/forum/general/18816/

回答by ivan.ukr

Attempt to access "f" will give you some result. It will likely be representation other member of union as data type of "f", i.e. in this case you are likely will be reading partial or entire content of the "pwszFoo" represented as "long" data type. General concept is easy - union members share the same location in memory.

尝试访问“f”会给你一些结果。它可能将联合的其他成员表示为“f”的数据类型,即在这种情况下,您可能会读取表示为“long”数据类型的“pwszFoo”的部分或全部内容。一般概念很简单——联合成员在内存中共享相同的位置。