title: How are objects stored in memory in C++?
body: <p>How are objects stored in memory in C++?</p>
  
  <p>For a regular class such as </p>
  
  <pre><code>class Object
      {
  public:
      int i1;
      int i2;
      char i3;
      int i4;
  private:
      };
  </code></pre>
  
  <p>Using a pointer of Object as an array can be used to access i1 as follows?</p>
  
  <pre><code>((Object*)&amp;myObject)[0] === i1?
  </code></pre>
  
  <p>Other questions on SO seem to suggest that casting a struct to a pointer will point to the first member for POD-types. How is this different for classes with constructors if at all?
  Also in what way is it different for non-POD types?</p>
  
  <p>Edit:</p>
  
  <p>In memory therefore would the above class be laid out like the following?</p>
  
  <pre><code>[i1 - 4bytes][i2 - 4bytes][i3 - 1byte][padding - 3bytes][i4 - 4bytes]
  </code></pre>