Hi folks,
I have a mathematic Vector3 class that looks like
class Vector3
{
public:
union
{
struct
{
float x;
float y;
float z;
// btw, if declared like
// float x, y, z;
// will there be a difference in packing order?
};
float v[3];
};
// insert many member and static functions here
}
I want to use it in another struct like this
struct Vertex
{
Vector3 p;
Vector3 n;
Vector3 c;
}
And then pass it to a function which accesses it sequentially
void work(void* p, void* n, void* c, int stride)
{
do {
doP(p); p += stride; // actually i made up this code. I don't know if you can
doN(n); n += stride; // increment void pointers like this... but you get the idea.
doC(C); n += stride;
} while (not end of sequence)
}
// calling work
Vertex v[1000];
work(&v[0].p, &v[0].n, &v[0].c, sizeof(Vertex));
My question is, with the above code, is it always safe to call assume the packing order is in the manner I call work() ?
How can I make the code safer and "portable" if that is the correct word to use. It will be great if you can point me to easy-to-understand readings on this topic. Perhaps on union/class/struct keyword in relation to this issue.