Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok you first question:I give you a example which may be better understanding! <img src="https://i.stack.imgur.com/UvLpU.jpg" alt="enter image description here"></p> <pre><code>#include&lt;iostream&gt; using namespace std; class Base1 { public: int ibase1; Base1():ibase1(10) {} virtual void f() { cout &lt;&lt; "Base1::f()" &lt;&lt; endl; } virtual void g() { cout &lt;&lt; "Base1::g()" &lt;&lt; endl; } virtual void h() { cout &lt;&lt; "Base1::h()" &lt;&lt; endl; } }; class Base2 { public: int ibase2; Base2():ibase2(20) {} virtual void f() { cout &lt;&lt; "Base2::f()" &lt;&lt; endl; } virtual void g() { cout &lt;&lt; "Base2::g()" &lt;&lt; endl; } virtual void h() { cout &lt;&lt; "Base2::h()" &lt;&lt; endl; } }; class Base3 { public: int ibase3; Base3():ibase3(30) {} virtual void f() { cout &lt;&lt; "Base3::f()" &lt;&lt; endl; } virtual void g() { cout &lt;&lt; "Base3::g()" &lt;&lt; endl; } virtual void h() { cout &lt;&lt; "Base3::h()" &lt;&lt; endl; } }; class Derive : public Base1, public Base2, public Base3 { public: int iderive; Derive():iderive(100) {} virtual void f() { cout &lt;&lt; "Derive::f()" &lt;&lt; endl; } virtual void g1() { cout &lt;&lt; "Derive::g1()" &lt;&lt; endl; } }; </code></pre> <p>This the memory rank of a derived class which implemented three base classes base1, base2, base3, where you :</p> <pre><code>Base1 *p1 = new Derive(); Base2 *p2 = new Derive(); Base3 *p3 = new Derive(); </code></pre> <p>p1 will point the vtale1, p2 will point to vtable2, p3 will point to vtable3, If you call some virtual function it will find the very virtual table and get the address!</p> <p>In your code:</p> <pre><code>X *h = new Y; </code></pre> <p>h will point to beginning position of the memory of Y, which is the virtual table of X,he will find the address of <code>abc()</code> which implemented in Y!</p> <p>your second question:</p> <p>The complier will consider the member function as the normal function, so it put the address of member function in the <code>code section</code>,so it does not occupied the memory!!</p> <p>If you want to read the virtual table you can try like this:which is I tried in my example in gcc4.7</p> <pre><code>typedef void(*Func)(void); Derive d; int **pd = (int **)(&amp;d); int i = 0; while(i &lt; 4) { Func f = (Func)pd[0][i]; f(); i++; } int s = (int)(pd[1]); cout &lt;&lt; s &lt;&lt; endl; i = 0; cout &lt;&lt; "===============================================" &lt;&lt; endl; while(i &lt; 3) { Func f = (Func)pd[2][i]; f(); i++; } s = (int)(pd[3]); cout &lt;&lt; s &lt;&lt; endl; cout &lt;&lt; "===============================================" &lt;&lt; endl; i = 0; while(i &lt; 3) { Func f = (Func)pd[4][i]; f(); i++; } s = (int)(pd[5]); cout &lt;&lt; s &lt;&lt; endl; s = (int)(pd[6]); cout &lt;&lt; s &lt;&lt; endl; </code></pre> <p>and you will get the result as follow:</p> <pre><code> Derive::f() Base1::g() Base1::h() Derive::g1() 10 =============================================== Derive::f() Base2::g() Base2::h() 20 =============================================== Derive::f() Base3::g() Base3::h() 30 100 </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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