I have a class to represent a 3D vector of floats:
class Vector3D
{
public:
float x, y, z;
float * const data;
Vector3D() : x(0.0), y(0.0), z(0.0), data(&x) {}
}
My question is: are x, y, and z going to be allocated sequentially in memory such that I can assign the address of x to data and later use the subscript operator on data to access the vector components as an array?
For example, sometimes I may want to access the vector components directly:
Vector3D vec;
vec.x = 42.0;
vec.y = 42.0;
vec.z = 42.0;
And sometimes I may want to access them by offset:
Vector3D vec;
for (int i = 3; i--; )
vec.data[i] = 42.0;
Will the second example have the same effect as the first one, or do I run the risk of overwriting memory other than the x, y, and z floats?