Here is a type I declared:
(I declared t_sphere
, t_cylinder
and t_triangle
too)
typedef struct s_intersection{
double t1;
double t2;
int id;
union {
t_sphere sph;
t_cylinder cyl;
t_triangle tri;
} u;
} t_intersection;
When I use that intersection structure in some code, is there a handy way to refer to the member inside my union ?
e.g. Let's say I want to write a function that acts differently according to the type of geometric_figure it contains. Will I have to do it like this ?
if (geometric_figure_id == SPHERE_ID)
// I know I will have to refer to p->u with p->u.sph...
else if(geometric_figure_id == CYLINDER_ID)
// I know I will have to refer to p->u with p->u.cyl...
else if (geometric_figure_id == TRIANGLE_ID)
// I know I will have to refer to p->u with p->u.tri...
What if I had 10 different geometric_figures types inside my union ?
This feels very heavy.
Do you have a more elegant solution ?