Unions

Started by
11 comments, last by masonium 22 years, 6 months ago
Exactly what is a union in a struct? Do they work with classes?
Advertisement
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! =)
.....Could you be a little clearer on that?
http://www.cplusplus.com/doc/tutorial/tut3-6.html

see the section on ''unions''
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.
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 !! }
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
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
can somebody run off some common uses for unions? I see how they work, but I don''t see any important uses...
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!]

This topic is closed to new replies.

Advertisement