I have a class vector
class vector3f
{
public:
float x;
float y;
float z;
};
If I want to access one of the members I have to type vec.x, vec.y and vec.z. Now, I have an algorithm that should access the data members per index, for example:
for(int i = 0; i < 3; i++)
{
vec[i] = i*i;
}
Doing this without an index would result in several if-conditions:
for(int i = 0; i < 3; i++)
{
if(i == 0){vec.x = i*i;}
else if(i == 1){vec.y = i*i;}
else if(i == 2){vec.z = i*i;}
}
Is there a way to index the data members so that they are resolved in compile-time? I don't want to use an array in the vector class because accessing the data members with vec.x, vec.y and vec.z is more convenient for the programmer. What would be the best solution to avoid these if-conditions?