If I learn this derived version with g ++
class X: public A, public B { unsigned magic; public: X() : magic(0xcafebabe) {}; virtual void func(){ var = 1; } int var; }; extern "C" int main() { X * x = new X;
It turns out that in C ++ b == a + 1, the structure X is [vtable-X + A] [vtable-B] [magic] [var] checking deeper (nm./a.out), vtable-X + a contains a link to X :: func (as you would expect). when you sent your X to B, it adjusted the pointers so that VTBL functions for B are displayed where the code expects this.
In fact, did you intend to "hide" B :: func ()?
B vtbl looks like binding a link to a "trampoline" to X, which restores the object pointer to full X before calling the "regular" X :: func, which has X + A vtbl.
080487ea <_ZThn8_N1X4funcEv>: # in "XB vtbl" _ZThn8_N1X4funcEv(): 80487ea: 83 44 24 04 f8 addl $0xfffffff8,0x4(%esp) 80487ef: eb 01 jmp 80487f2 <_ZN1X4funcEv> 80487f1: 90 nop 080487f2 <_ZN1X4funcEv>: # in XA vtbl _ZN1X4funcEv(): 80487f2: 55 push %ebp 80487f3: 89 e5 mov %esp,%ebp 80487f5: 8b 45 08 mov 0x8(%ebp),%eax 80487f8: c7 40 14 01 00 00 00 movl $0x1,0x14(%eax) 80487ff: 5d pop %ebp 8048800: c3 ret
Pypebros
source share