class B {
public:
int b1;
int b2;
virtual void f1 () { ... }
virtual void f2 () { ... }
};
class D : public B {
public:
int d1;
int d2;
virtual void f2 () { ... }
virtual void f3 () { ... }
};
D *d = new D ();
d->b1 = 2948;
d->f1 ();
d->f2 ();
d->f3 ();
B *b = d;
b->f1 ();
b->f2 ();
|
|
movq -24(%rbp), %rax
movl $2948, 8(%rax)
movq -24(%rbp), %rdx
movq (%rdx), %rdx ; get vtable
movq (%rdx), %rdx ; get method
movq -24(%rbp), %rax
movq %rax, %rdi ; store "this"
call *%rdx
movq -24(%rbp), %rax
movq (%rax), %rax ; get vtable
addq $8, %rax ; f2 offset
movq (%rax), %rdx ; get method
movq -24(%rbp), %rax
movq %rax, %rdi ; store "this"
call *%rdx
movq -24(%rbp), %rax
movq (%rax), %rax ; get vtable
addq $16, %rax ; f3 offset
movq (%rax), %rdx ; get method
movq -24(%rbp), %rax
movq %rax, %rdi ; store this
call *%rdx
; B *b = d;
movq -24(%rbp), %rax
movq %rax, -32(%rbp)
movq -32(%rbp), %rax
movq (%rax), %rax ; get vtable
movq (%rax), %rdx ; get method
movq -32(%rbp), %rax
movq %rax, %rdi ; store "this"
call *%rdx
movq -32(%rbp), %rax
movq (%rax), %rax ; get vtable
addq $8, %rax ; offset
movq (%rax), %rdx ; get method
movq -32(%rbp), %rax
movq %rax, %rdi ; store "this"
call *%rdx
|
|
Layout of Stack
|
Offset
|
Contents
|
-24(%rbp)
|
local variable D *d
|
-32(%rbp)
|
local variable B *b
|
Layout of Instance of D
|
Offset
|
Name
|
Contents
|
20 | d2 | 3293 |
16 | d1 | 9032 |
12 | b2 | 8432 |
8 | b1 | 2948 |
0 | vtable | address of vtable of class D |
Layout of Vtable of Class D
|
Offset
|
Name
|
Contents
|
16 | f3 | address of D.f3 code |
8 | f2 | address of D.f2 code |
0 | f1 | address of B.f1 code |
|