Quote:Original post by BrickInTheWall
thanks I think I kinda understand...but I don't quite get all of it :D
If I set x.fp to 2 for example and print x.hi and x.lo I get some very large number and 0 (for x.lo)...what does this mean...I can't quite understand this yet...whatever I enter for x.fp ...x.lo is always 0, x.hi is always a very large number.
It is undefined behavior to access a type in a union if it wasn't the last one written. Unions allow you to store multiple types in the same memory location. This makes for a cheap "any" datatype. As an example
class keyboard_event{
public:
int type;
int char_pressed;
};
class mouse_event{
int type;
int x,y;
}
union event{
keyboard_event;
mouse_event;
};
event getLastEvent();
I get a "polymorphic" return type since I can return either a keyboard or mouse event with out having to do a bunch of manual casting. I don't have to make my events inherit from some base class just to make casting work. I use the type variable present in each event to figure out which type of event it was.