I've known what a union is for a while now, but i've wondered if there is a way to do this:
class Vector
{
union
{
Scalar i[3];
Scalar X;
Scalar Y;
Scalar Z;
};
}
It compiles, but (I haven't checked why) but it doesn't work. I would think would mean I could do this:
Vector myVector;
myVector.i[0] = 1;
myVector.i[1] = 2;
myVector.i[2] = 3;
cout << myVector.X << myVector.Y << myVector.Z << endl;
The output I thought would be 123, but it is infact 111. Anyone have any ideas?
Vector myVector;
myVector.X = 1;
myVector.Y = 2;
myVector.Z = 3;
std::cout << myVector.X << myVector.Y << myVector.Z << std::endl;
From this I get the output of 333. There is something really odd.
Edit: Ok, nevermind, i've realised why it won't work, although is there a way I can do this at all, without the need to have a struct for X, Y and Z?