union related q?

how to find out which member of union is currently set/active.
exm:
union myunion{
int i;
float f;

}

if i set myunion ob{7}, it set ob.i=7;
my q? is how to find out i is set f is not set/ not acrive
The first time I saw unions used well is in the SDL project for the Event union.
The union contains a type variable. You might want to add a type variable to your union to be able to tell what it is holding. You will have to have everything else in the union be a struct, and all those structs need the same type variable as the first thing inside their definitions.
Here is a link to the SDL documentation for an example:
http://sdl.beuc.net/sdl.wiki/SDL_Event
Or, create a structure that contains both the identifier and the union.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum TypeIdentifier
{
    INT,
    FLOAT
};

union MyUnion
{
    int i;
    float f;
};

stuct Data
{
    TypeIdentifier type;
    MyUnion data;
};


You can also define the enum and union anonymously inside the struct if you want to.
Topic archived. No new replies allowed.