title: C++ Index class members
body: <p>I have a class vector</p>
  
  <pre><code>class vector3f
  {
  public:
  float x;
  float y;
  float z;
  };
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>for(int i = 0; i &lt; 3; i++)
  {
  vec[i] = i*i;
  }
  </code></pre>
  
  <p>Doing this without an index would result in several if-conditions:</p>
  
  <pre><code>for(int i = 0; i &lt; 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;}
  }
  </code></pre>
  
  <p>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? </p>