I came across a library called PolyBoolean. This is essentially irrelevant to my question. In the library there is a strange struct type as follows:
struct VNODE2
{
VNODE2 * next;
VNODE2 * prev;
UINT32 Flags;
union
{
VECT2 p;
GRID2 g;
};
};
and GRID2 is defined as:
struct GRID2
{
INT32 x, y;
};
The GRID2 defines a point, VECT2 is another representation of a point similar to GRID2 and VNODE2 is a node. Say I have a VNODE2 structure variable v2, how can I get the value of x and y in g?
I tried v2.g.x, the vs2008 compiler gave me an error: "error C2059: syntax error" Any help would be greatly appreciated.
this is anonymous union, so you refer to members just as they are, see:

be sure that you declared GRID2 before your struct. Here is working example. You can also name your union to create instance in your class.
typedef int UINT32;
typedef int VECT2;
typedef int INT32;
struct GRID2
{
INT32 x, y;
};
struct VNODE2
{
VNODE2 * next;
VNODE2 * prev;
UINT32 Flags;
union
{
VECT2 p;
GRID2 g;
};
};
int main(int argc, char** argv) {
VNODE2 v;
v.g.x=1;
return 0;
}
in case if you would like your union to have name do it as follows:
struct GRID2
{
INT32 x, y;
};
struct VNODE2
{
VNODE2 * next;
VNODE2 * prev;
UINT32 Flags;
union
{
VECT2 p;
GRID2 g;
}myNamedUnion;
};
int main(int argc, char** argv) {
VNODE2 v;
v.myNamedUnion.g.x=1;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With