views:

119

answers:

4

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 hr directly? If I set hr, what happens if I try to access f?

+1  A: 

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

To access hr:

Odp o1;
o1.hr;

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

John Weldon
In this case, the struct doesn't have a name. How would I access it?
Rosarch
@Rosarch: Technically it's illegal in C. It's legal in C++, in which case the names are exposed as members of `class Odp`.
Billy ONeal
Nice catch @Billy
John Weldon
@Billy ONeal: Incorrect. "Anonymous structs" are illegal in C++. C++ supports anonymous unions, but not anonymous structs. The OP's declaration is ill-formed.
AndreyT
The link in my answer suggests otherwise...
John Weldon
@John Weldon: The link in your answer doesn't suggest anything, until you get to "jsmith's" post, which correctly states that the code is illegal.
AndreyT
I concede, given that I don't know the answer definitively :)
John Weldon
@John Weldon: In your answer, `Odp o1();` declares a function, not an object. Somehow It think this wasn't your intent.
AndreyT
@AndreyT; do you want to review any of my other answers? You're on a streak :D
John Weldon
@AndreyT: D'oh! Thanks :)
Billy ONeal
+1  A: 

Right, with a union the 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.

Try using the following to access hr:

union a;
a.hr = NULL;
Justin Ethier
So it's up to the programmer to guard against it, or will an exception be thrown?
Rosarch
@Rosarch: This is a C feature; C does not have exceptions. Use unions at your own risk :)
Billy ONeal
No exception will be thrown; it is up to the programmer to make sure they are accessing the proper member of the `union`.
Justin Ethier
+3  A: 

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.

AndreyT
Hmm... I knew the result would be to return undefined data, but AFAIK the act of accessing the other member shouldn't cause undefined behavior like accessing the target of a null pointer does.
Billy ONeal
@Billy ONeal: Yes, it should, for obvious reasons. Since the types are generally unrelated, the other (inactive) member can easily end up with trap representation. Which is the main (and obvious) reason the behavior is undefined.
AndreyT
"trap representation" <-- Never heard of that one before. +1
Billy ONeal
+3  A: 

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:

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.

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

anon
+1 -- but even for C libraries `reinterpret_cast` is usable instead of a union most of the time.
Billy ONeal