Jump to content
  • Advertisement
Sign in to follow this  
Richy2k

C++ and Unions

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
union { float v[3]; struct { float x, y, z; }; };

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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. :/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!