title: Classes store data members in sequential memory?
body: <p>The short version of this question: Does a pointer to the first data member of a class result in a pointer to all its data members?</p>
  
  <p>Discussion:</p>
  
  <p>I'm reading some sample code, and this is interesting. When a particular function (in this case the <code>glUniformMatrix4fv</code> C function from OpenGL) takes as a parameter an array, this is passed in the common C way of using a pointer to the address of the array's first element, as follows:</p>
  
  <p><code>glUniformMatrix4fv(glvariable, 1, 0, &amp;newmatrix[0]);</code>  // referring to newmatrix</p>
  
  <p>This developer has a C++ class for creating a 4X4 matrix, for a total of 16 floats. However, the data members of that class are divided into 4 separate vectors of 4 data members each:</p>
  
  <p>Here are the data members of the class:</p>
  
  <pre><code>vec4 x;
  vec4 y;
  vec4 z;
  vec4 w;
  </code></pre>
  
  <p>The <code>vec4</code> class in turn provides these data members:</p>
  
  <pre><code>T x;
  T y;
  T z;
  T w;
  </code></pre>
  
  <p>He then creates a function to point to just the first <code>vec4 x</code> data member:</p>
  
  <pre><code>    const T* Pointer() const
  {
      return &amp;x.x;
  }
  </code></pre>
  
  <p>And somehow, this miraculously translates into sending all 4 <code>vec4</code> objects and their respective 4 components:</p>
  
  <pre><code>glUniformMatrix4fv(modelviewUniform, 1, 0, modelviewMatrix.Pointer());
  </code></pre>
  
  <p>I'm fairly new to C++ but this surprised me that you can point to just one data member and expect all the rest to get sent as well. Is it because the class defines them in a particular order? What if they had been defined in a different order in the class definition?</p>