Archived

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

masonium

Unions

Recommended Posts

MARS_999    1627
Unions are their own type. They if I remember right can hold only one type at a time? e.g. int or float but not both. Hope that helps? And pretty sure you can use them in classes just like any other data type.

Please forgive me! =)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
http://www.cplusplus.com/doc/tutorial/tut3-6.html

see the section on ''unions''

Share this post


Link to post
Share on other sites
Zipster    2365
A union is a group of variables that you want to share the same memory. When you assign a value to one, all the other ones are invalidated, and you can only access one at a time. The amount of space a union actual takes is determined by the size of the largest member.

Share this post


Link to post
Share on other sites
jwalker    122
well

  

union
{
int x;
char y;
}


void myfunc()
{
x = 65;

// will print A

printf("%c",y);
}



all elements of a union share the same memory space.
declaration of a union is same as a struct




{ Stating the obvious never helped any situation !! }

Share this post


Link to post
Share on other sites
MARS_999    1627
Wrong on that union example

union surface
{
int a;
float b;
char a;
};

surface front_surface;

front_surface.a = 1;
cout << front_surface.a << endl;

//this will display 1

//if you define char a like this
front_surface.c = 'A';
cout << front_surface.c << endl;
//you will get a A displayed

//if you define variable b and a also you will get garbage
//you do this you will get garbage
front_surface.a = 1;
front_surface.b = 1.00F;
front_surface.c = 'A';
cout << front_surface.a << endl;
cout << front_surface.b << endl;
cout << front_surface.c << endl;

//just define only one variable at a time not two or three or more

hope that helps
from what I have read unions are used to help save memory? But I could be wrong. And if thats the case look at my signature!

Please forgive me! =)

Edited by - mars_999 on October 17, 2001 3:54:50 PM

Share this post


Link to post
Share on other sites
brad_beveridge    122
Yip, unions are used to save memory - but you don't want to think of it as "invalidating data" when you change it. For example
      
union {
struct {
unsigned short highword;
unsigned short loword;
}

unsigned int wholeInt;
}


I can access the top or bottom 16 bits of the union with highword/loword, or the whole 32bits with wholeInt. In this case I am using a union and struct as tricks for data access.
You also commonly see
        
struct {
union{
float f;
int i;
}

bool isFloat;
}

You have a union within a struct and use the variable isFloat to determine the state of the union.

Hope it helps
Brad

Edited by - brad_beveridge on October 17, 2001 7:08:59 PM

Edited by - brad_beveridge on October 17, 2001 7:10:15 PM

Edited by - brad_beveridge on October 17, 2001 7:11:00 PM

Edited by - brad_beveridge on October 17, 2001 7:12:05 PM

Share this post


Link to post
Share on other sites
Null and Void    1088
The way I (and XLib, coincidentally) use them is like this: To get information about any even that happens you have to pass a pointer. The event function will copy the information into this pointer, and you''ll use a certain part of it depending upon what the event is. Now, this is where unions come in. I don''t know what event will occur, so I don''t know what size the struct I have to pass is. So, I pass a union that looks like this (sort of):
  
union EventThingy {
struct {
int X, Y, Action;
} Mouse;
struct {
int Key;
} Keyboard;
};

Now, if I find out that the event happened to be a Mouse event, I know that EventThingy.Mouse will have valid data. This prevents me from declaring 10 different structs, one for each event, and saves memory.

Another common use is like this:
  
union Vertex {
float V[3];
struct {
float X, Y, Z;
};
};

That only uses 12 bytes (like normal), but now you can use the vertex as either an array or use as an individual element. This is also common with Matrices.

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Unions also have another BIG role in almost all of Microsoft''s new technologies.

In the COM world, the VARIANT data type is a giant union!! Cool, isn''t it That''s why it can store all those different data types.

-nt20

Share this post


Link to post
Share on other sites
Yanroy    122
I''ve always known what unions were, but never what to do with them... The example of having both a float and an int in the same one gave me a great idea (probably not a new one). I always make horribly complicated classes for variables in my scripting language(s) that remember what type they are and behave accordingly. I could replace all that with a simple union! hehehe thx

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
An alternative to the RGB macro...

  
union RGB
{
unsigned long composite;
struct
{
unsigned char r;
unsigned char g;
unsigned char b;
}
}

//..........


RGB myColor;

myColor.r = 82;
myColor.g = 255;
myColor.b = 9;

if(myColor.composite == RGB(82, 255, 9))
{
//This code WILL execute...

}


Share this post


Link to post
Share on other sites