title: memory layout of inherited class
body: <p>I'm keen to know exactly how the classes will be arranged in memory esp. with inheritance and virtual functions. </p>
  
  <p>I know that this is not defined by the c++ language standard. However, is there any easy way to find out how your specific compiler will implement these say by writing some test code? </p>
  
  <p>EDIT:- Using some of the answers below :- </p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class A {
    public:
      int a;
      virtual void func() {}
  };
  
  class B : public A {
    public:
      int b;
      virtual void func() {}
  };
  
  class C {
    public:
      int c;
      virtual void func() {}
  };
  
  class D : public A, public C {
    public:
      int d;
      virtual void func() {}
  };
  
  class E : public C, public A {
    public:
      int e;
      virtual void func() {}
  };
  
  class F : public A {
    public:
      int f;
      virtual void func() {}
  };
  
  class G : public B, public F {
    public:
      int g;
      virtual void func() {}
  };
  
  int main() {
    A a; B b; C c; D d; E e; F f; G g;
    cout&lt;&lt;"A: "&lt;&lt;(size_t)&amp;a.a-(size_t)&amp;a&lt;&lt;"\n";
    cout&lt;&lt;"B: "&lt;&lt;(size_t)&amp;b.a-(size_t)&amp;b&lt;&lt;" "&lt;&lt;(size_t)&amp;b.b-(size_t)&amp;b&lt;&lt;"\n";
    cout&lt;&lt;"C: "&lt;&lt;(size_t)&amp;c.c-(size_t)&amp;c&lt;&lt;"\n";
    cout&lt;&lt;"D: "&lt;&lt;(size_t)&amp;d.a-(size_t)&amp;d&lt;&lt;" "&lt;&lt;(size_t)&amp;d.c-(size_t)&amp;d&lt;&lt;" "&lt;&lt;(size_t)&amp;d.d-    (size_t)&amp;d&lt;&lt;"\n";
    cout&lt;&lt;"E: "&lt;&lt;(size_t)&amp;e.a-(size_t)&amp;e&lt;&lt;" "&lt;&lt;(size_t)&amp;e.c-(size_t)&amp;e&lt;&lt;" "&lt;&lt;(size_t)&amp;e.e-    (size_t)&amp;e&lt;&lt;"\n";
    cout&lt;&lt;"F: "&lt;&lt;(size_t)&amp;f.a-(size_t)&amp;f&lt;&lt;" "&lt;&lt;(size_t)&amp;f.f-(size_t)&amp;f&lt;&lt;"\n";
    cout&lt;&lt;"G: "&lt;&lt;(size_t)&amp;g.B::a-(size_t)&amp;g&lt;&lt;" "&lt;&lt;(size_t)&amp;g.F::a-(size_t)&amp;g&lt;&lt;" "    &lt;&lt;(size_t)&amp;g.b-(size_t)&amp;g&lt;&lt;" "&lt;&lt;(size_t)&amp;g.f-(size_t)&amp;g&lt;&lt;" "&lt;&lt;(size_t)&amp;g.g-(size_t)&amp;g&lt;&lt;"\n";
  }
  </code></pre>
  
  <p>And the output is :- </p>
  
  <pre><code>A: 8
  B: 8 12
  C: 8
  D: 8 24 28
  E: 24 8 28
  F: 8 12
  G: 8 24 12 28 32
  </code></pre>
  
  <p>So all classes have got a v-ptr at loc 0 of size 8. 
  D has another v-ptr at location 16. Similarly for E. 
  G also seems to have a v-ptr at 16 although from my (limited) understanding I would have guessed it to have more. </p>