title: vtable and polymorphism - offset of a function
body: <p>If I understand things correctly, a class definition imposes a certain order of the virtual functions in the vtable, and so a given function is known to be at a certain offset from the beginning of the table. However, I don't understand how that works with polymorphism.</p>
  
  <pre><code>class B1 {
    virtual void funcB1();
  };
  
  class B2 {
    virtual void funcB2() {}
  };
  
  class D : public B1, public B2 {
    virtual void funcB1() {}
    virtual void funcB2() {}
  };
  
  void main(...) {
    B1 *b1 = new D();
    B2 *b2 = new D();
    B1 *realB1 = new B1();
    B2 *realB2 = new B2();
  
    b1-&gt;funcB1();
    b2-&gt;funcB2();
    realB1-&gt;funcB1();
    realB2-&gt;funcB2();
  }
  </code></pre>
  
  <p>How does the generated code know how to access funcB2 at different offsets?</p>