C++ and Unions

Started by
17 comments, last by Zahlman 18 years, 8 months ago
I've known what a union is for a while now, but i've wondered if there is a way to do this:

class Vector
{
    union
    {
        Scalar i[3];
        Scalar X;
        Scalar Y;
        Scalar Z;
    };
}



It compiles, but (I haven't checked why) but it doesn't work. I would think would mean I could do this:

Vector myVector;

myVector.i[0] = 1;
myVector.i[1] = 2;
myVector.i[2] = 3;

cout << myVector.X << myVector.Y << myVector.Z << endl;



The output I thought would be 123, but it is infact 111. Anyone have any ideas?

	Vector myVector;

	myVector.X = 1;
	myVector.Y = 2;
	myVector.Z = 3;

	std::cout << myVector.X << myVector.Y << myVector.Z << std::endl;


From this I get the output of 333. There is something really odd. Edit: Ok, nevermind, i've realised why it won't work, although is there a way I can do this at all, without the need to have a struct for X, Y and Z?
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
Advertisement
union { float v[3]; struct { float x, y, z; }; };
Nope, you need to create a struct to house X/Y/Z.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/class_29.asp has a diagram showing how the memory space overlaps in a union.
-Scott
Anon Poster: You were right, works spot on!
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
Watch out, this is a Visual Studio-specific extension. In Standard C++, you can't just have nameless unions (or struct) as part of a struct and expect to be able to access their members.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You are kidding me [crying] please say its April 1st. I was really hoping there was a way to do it. Argh, /me goes and fishes out knoppix cd to try in gcc.
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
struct sXYZ{   Scalar X;   Scalar Y;   Scalar Z;};union uVector{   Scalar i[3];   sXYZ s;};

One way of many ways. This should work w/ any c++ compiler. (iirc ;) )
Edit:
Alternate methods could use Anonymous unions. Not my style but probably what your looking for.
Quote:Original post by Fruny
Watch out, this is a Visual Studio-specific extension. In Standard C++, you can't just have nameless unions (or struct) as part of a struct and expect to be able to access their members.

You can have unlabled unions when they are inside a stuct/class.
Edit2: Should be just inside structs. :/
-Scott
check this
There is a way if you dont mind extra dots:
class Vector{union vector { float v[3]; struct { float x, y, z; }; };};


extra dots ensue:

Vector point;
point.vector.v[0]=1.0f;
point.vector.y=1.0f;
etc.
I was thinking that way, but that would mean rewriting a lot of my code. I'm too far into it to change to using that, i'm probably going to convert it to reference vectors by going 'myVector.i[0]' etc instead anyways, so hopefully I can ditch the incompatible anon union thing...
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd

This topic is closed to new replies.

Advertisement