title: C++ multiple level inheritance thunk object pointer adjustment
body: <p>Consider the following code with 3-level multiple inheritance hierachy.</p>
  
  <pre><code>auto addr = [](auto v) -&gt; uint64_t { return *reinterpret_cast&lt;uint64_t*&gt;(v); };
  
  struct BaseA
  {
      void virtual a() {}
  };
  
  struct BaseB
  {
      void virtual b() {}
  };
  
  struct BaseC : BaseA, BaseB
  {
      void virtual a() override {}
      void virtual b() override {}
  };
  
  struct BaseD
  {
      void virtual d() {}
  };
  
  struct BaseE : BaseD, BaseC
  {
      void virtual d() override {}
      void virtual a() override {}
      void virtual b() override { auto a = this; std::cout &lt;&lt; "called here: " &lt;&lt; addr(&amp;a) &lt;&lt; "\n"; }
  };
  
  int main()
  {
  
      BaseE obj;
  
      BaseE* ePtr = &amp;obj;
      BaseD* dPtr = &amp;obj;
      BaseC* cPtr = &amp;obj;
      BaseB* bPtr = &amp;obj;
      BaseA* aPtr = &amp;obj;
  
      ePtr-&gt;b();
      cPtr-&gt;b();
      bPtr-&gt;b();
  
      std::cout &lt;&lt; "e is at: " &lt;&lt; addr(&amp;ePtr) &lt;&lt; "\n"
                &lt;&lt; "d is at: " &lt;&lt; addr(&amp;dPtr) &lt;&lt; "\n"
                &lt;&lt; "a is at: " &lt;&lt; addr(&amp;aPtr) &lt;&lt; "\n"
                &lt;&lt; "c is at: " &lt;&lt; addr(&amp;cPtr) &lt;&lt; "\n"
                &lt;&lt; "b is at: " &lt;&lt; addr(&amp;bPtr) &lt;&lt; "\n"
                &lt;&lt; "total size is " &lt;&lt; sizeof(BaseE) &lt;&lt; "\n"
                &lt;&lt; "vptr D and E " &lt;&lt; vpt1 &lt;&lt; "\n"
                &lt;&lt; "vptr A and C " &lt;&lt; vpt2 &lt;&lt; "\n"
                &lt;&lt; "vptr B " &lt;&lt; vpt3 &lt;&lt; "\n";
  return 0;
  }
  </code></pre>
  
  <p>The output from this code run is the following:</p>
  
  <pre><code>called here: 140736308965696
  called here: 140736308965696
  called here: 140736308965696
  e is at: 140736308965696
  d is at: 140736308965696
  a is at: 140736308965704
  c is at: 140736308965704
  b is at: 140736308965712
  total size is 24
  vptr D and E 4390608
  vptr A and C 4390648
  vptr B 4390680
  </code></pre>
  
  <p>This suggest the following memory layout for BaseE obj (with the pointer size being 8 bytes).</p>
  
  <hr>
  
  <h2>8 bytes, BaseD subobject, vptr to D table only</h2>
  
  <h2>8 bytes, BaseA subobject, vptr to A table only</h2>
  
  <h2>8 bytes, BaseB subobject, vptr to B table only.</h2>
  
  <p>Here <code>ePtr</code> and <code>dPtr</code> both point to <code>BaseD</code> subobject, both <code>aPtr</code> and <code>cPtr</code> point to <code>BaseA</code> subobject and <code>bPtr</code> to <code>BaseB</code> subobject.</p>
  
  <p>My question is now is what thunk code will the compiler generate to adjust the <code>this</code> pointer in two calls above to <code>b()</code> through pointers <code>cPtr</code> and <code>bPtr</code> to make sure <code>this</code> points correctly to <code>ePtr</code> when <code>BaseE</code> implementation of <code>b()</code> is called? Since <code>cPtr</code> and <code>bPtr</code> have different address does the thunk need to be aware to adjust differently based on what type of Base class pointer is passed to it? </p>