I'm making a 4x4 Matrix class, with the data internally represented as an array of 16 floats. Sometimes I may want to access this array directly, but other times I may just want to access an array slot by name.
class Matrix{
union{
float array[16];
struct{
float a1, a2, a3, a4;
float b1, b2, b3, b4;
float c1, c2, c3, c4;
float d1, d2, d3, d4;
}named;
struct{
float a[4];
float b[4];
float c[4];
float d[4];
}row;
}
}
// use
m.array[3] = 1.5f;
m.named.d1 = m.row.a[3];
printf("%f", m.named.d1); // should be 1.5
Is this type of thing sane, or will it massively backfire in ways I cannot begin to dream of?
It seems like the later, but I really enjoy having multiple access methods.
This is for manipulating 3D points for graphics stuff. It's a separate question in itself, but any suggestions for a library that has these things already figured out is entirely welcome.