Note that there are some explanatory texts on larger screens.

plurals
  1. POVirtual tables and memory layout in multiple virtual inheritance
    primarykey
    data
    text
    <p>Consider following hierarchy:</p> <pre><code>struct A { int a; A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout &lt;&lt; i; } }; struct B1 : virtual A { int b1; B1(int i) : A(i) { f(i); } virtual void f(int i) { cout &lt;&lt; i+10; } }; struct B2 : virtual A { int b2; B2(int i) : A(i) { f(i); } virtual void f(int i) { cout &lt;&lt; i+20; } }; struct C : B1, virtual B2 { int c; C() : B1(6),B2(3),A(1){} virtual void f(int i) { cout &lt;&lt; i+30; } }; </code></pre> <ol> <li><p>What's the <strong>exact</strong> memory layout of <code>C</code> instance? How many vptrs it contains, where exactly each of them is placed? Which of virtual tables are shared with virtual table of C? What exactly each virtual table contains? </p> <p>Here how I understand the layout: </p> <pre><code>---------------------------------------------------------------- |vptr1 | AptrOfB1 | b1 | B2ptr | c | vptr2 | AptrOfB2 | b2 | a | ---------------------------------------------------------------- </code></pre> <p>where <code>AptrOfBx</code> is the pointer to <code>A</code> instance that <code>Bx</code> contains (since the inheritance is virtual).<br> Is that correct? Which functions <code>vptr1</code> points to? Which functions <code>vptr2</code> points to?</p></li> <li><p>Given the following code</p> <pre><code>C* c = new C(); dynamic_cast&lt;B1*&gt;(c)-&gt;f(3); static_cast&lt;B2*&gt;(c)-&gt;f(3); reinterpret_cast&lt;B2*&gt;(c)-&gt;f(3); </code></pre> <p>Why all the calls to <code>f</code> print <code>33</code>?</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload