Archived

This topic is now archived and is closed to further replies.

sab3156

Unions in C++

Recommended Posts

What the hell is the point of using Unions? Can anybody write some example code? I don''t really get it. Also, is it true that the members of the Union are stored in the same memory location? That REALLY bugs me.

Share this post


Link to post
Share on other sites

  
class Vector
{
public:
union
{
struct { float x, y, z; };
float v[3];
};

// blablablabla

}


Yes, I know it can cause problems to use anon structs, but it was the first thing that came to my mind and it works on the compilers I use.

Share this post


Link to post
Share on other sites
Yes, they are stored in the same memory location.

Here''s some example code from my class ''Vector'':


  
union VectorUnion
{
struct VectorStruct
{
float x;
float y;
float z;
float w;
}v;
float vector[4];
}t;


To clear up code I use the x,y,z... values in my functions. But sometimes I need the array, for example when passing the vector to a OpenGL function like glVertex4f.

Without the union I''d have to copy data from the variables to the array and vice verca each time one of ''em changed. This way I don''t.

Share this post


Link to post
Share on other sites
This is the default constructor of my class. If you know how to work with structs this should be familiar.


  
Vector::Vector(void)
{
t.v.x=0;
t.v.y=0;
t.v.z=0;
t.v.w=0;
}


Now, instead of writing

t.v.x=0;

you could also write

t.vector[0]=0;

It''s simply:

''x == vector[0]''
''y == vector[1]''
''z == vector[2]''
''w == vector[3]''

Share this post


Link to post
Share on other sites

You should use initializer lists in constructors as often as possible; especially for a small, often used class like this:

Vector::Vector(void) :
t.v.x ( 0 ),
t.v.y ( 0 ),
t.v.z ( 0 ),
t.v.w ( 1 )
{
}

Share this post


Link to post
Share on other sites
What's the advantage, except for making it look more cryptic?
I've never heard of initializing members of a struct that way.

edit: Didn't work...

[edited by - Wildfire on December 27, 2002 1:34:03 PM]

Share this post


Link to post
Share on other sites
Unions are very useful sometimes. In our scripting system, the structure that is used to send events to the game contains a union of the data structs from each event type. So you don''t have a giant class where you are only using 1% of the member variables at any one time, the class is much much smaller because its only the size of the largest member. Not sure if this made sense. Unions are kind of tricky when you first use them because they aren''t used frequently.

Share this post


Link to post
Share on other sites
Thanks for the link Prosper...

Well, increased performance is nice, but I still can't get it to work with the above example.
The compiler complains about having nested classes inside the list. So I guess it won't work when using structs inside classes.

Besided that:
quote:

Note: There is no performance difference if the type of x_ is some built-in/intrinsic type, such as int or char* or float.



And I guess you'll get an even better performance boost if you avoid initializing classes inside the main loop anyways... instead of passing classes via copy constructor -> const reference etc.

edit: something didn't make sense

[edited by - Wildfire on December 27, 2002 2:21:52 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by BitMaster
Yes, I know it can cause problems to use anon structs

Just curious, what kind of problems?

Share this post


Link to post
Share on other sites
Anon: Do something like this inside your class:


  
static struct
{
bla bla
}


Because the struct is static you''ll have to initialize it''s members by using the struct name, which you can''t because it doesn''t have one.

Share this post


Link to post
Share on other sites
quote:
What''s the advantage, except for making it look more cryptic?
I''ve never heard of initializing members of a struct that way.


- Base class with no default constructor. Initialization lists are the only way to handle that.
- Constant member variables. Initialization lists are the only way to set them.

To name a few.

Share this post


Link to post
Share on other sites
in my opinion, its not that useful with vectors, but its really nice with matrices for recursive functions:


union
{
float m44[4][4];
float m16[16];
struct
{
float _11,_12,_13,_14,
_21,_22,_23,_24,
_31,_32,_33,_34,
_41,_42,_43,_44;
};

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.