Note that there are some explanatory texts on larger screens.

plurals
  1. POunderstanding c++ vtables and RTTI
    primarykey
    data
    text
    <p><br> <br> I was messing around with vtables lately in order to better understand what a compiler/the process needs to do to implement classes and inheritance.<br> <br> <strong>This is what I was trying to accomplish:</strong><br> I wanted to write my own little vtable in order to force a static behaviour on an object:<br></p> <pre><code>class A { public: virtual void foo() { cout &lt;&lt; "A.foo()" &lt;&lt; endl; } virtual void bar() { cout &lt;&lt; "A.bar()" &lt;&lt; endl; } }; class B : public A { public: void foo() { cout &lt;&lt; "B.foo()" &lt;&lt; endl; } void bar() { cout &lt;&lt; "B.bar()" &lt;&lt; endl; } }; typedef void (A::*func)(); int main() { A&amp; b_as_a = *(new B()); long* p = (long*)(&amp;b_as_a); func* vtab = (func*)(p[0]); b_as_a.foo(); b_as_a.bar(); func* my_vtab = new func[4]; my_vtab[0] = vtab[0]; // \ I added these lines in step two after i got an my_vtab[1] = vtab[1]; // / access violation my_vtab[2] = &amp;A::bar; my_vtab[3] = &amp;A::foo; p[0] = (long)(my_vtab); b_as_a.foo(); b_as_a.bar(); delete[] my_vtab; delete &amp;b_as_a; return EXIT_SUCCESS; } </code></pre> <p>and here a dump of <code>g++ -std=c++11 -fdump-class-hierarchy</code></p> <pre><code>Vtable for A A::_ZTV1A: 4u entries 0 (int (*)(...))0 8 (int (*)(...))(&amp; _ZTI1A) 16 (int (*)(...))A::foo 24 (int (*)(...))A::bar Class A size=8 align=8 base size=8 base align=8 A (0x0x7f40b60fe000) 0 nearly-empty vptr=((&amp; A::_ZTV1A) + 16u) Vtable for B B::_ZTV1B: 4u entries 0 (int (*)(...))0 8 (int (*)(...))(&amp; _ZTI1B) 16 (int (*)(...))B::foo 24 (int (*)(...))B::bar Class B size=8 align=8 base size=8 base align=8 B (0x0x7f40b60dfbc8) 0 nearly-empty vptr=((&amp; B::_ZTV1B) + 16u) A (0x0x7f40b60fe060) 0 nearly-empty primary-for B (0x0x7f40b60dfbc8) </code></pre> <p><br> This did not work... so I researched a little.<br> I found this post: <a href="https://stackoverflow.com/questions/5712808/understanding-the-vtable-entries">What is the first (int (*)(...))0 vtable entry in the output of g++ -fdump-class-hierarchy?</a> <br> It explains the first two entries in a vtable. I understood what the first one does but all I know about the second entry is, that it's some sort of pointer to class information.<br> I guess that is the reason why it doesn't work.<br> <br> <strong>The question that remains is:</strong><br> What does the second entry in the vtable do that the funktionpointers underneath aren't read anymore???<br> <br> <br> Extra info: I am using g++ on openSuse 12.3</p>
    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.
 

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