What does "<vtable for A + 16>" mean?
This is my code. I just want to take a look at the virtual inheritance memory layout.
#include<iostream>
using namespace std;
class A{
private:
int a;
public:
virtual void print() const{
cout << a << endl;
}
};
class B:public virtual A{
private:
int b;
public:
void print() const{
cout << b << endl;
}
};
int main(){
A a;
B b;
return 0;
}
Then in gdb I used
p a
p b
Output signal
(gdb) p a
$1 = {
_vptr.A = 0x400b40 <vtable for A+16>,
a = 0
}
(gdb) p b
$2 = {
<A> = {
_vptr.A = 0x400b18 <vtable for B+56>,
a = 4196384
},
members of B:
_vptr.B = 0x400af8 <vtable for B+24>,
b = 0
}
(gdb)
I know the meanings of _vptr.A and _vptr.B, but I don’t understand what vtable means for B + 24 or + 16.
Thanks for the answer!
+4
1 answer
The list asays that it is a class with two fields: a vptr, which points to a byte with an offset of 16 inside the vtable for A and a data item acontaining zero.
The second - along the same lines, but more complex due to the virtual base class. He says that b consists of three fields: an instance of A with fields similar to those indicated above, and then two elements of B, again as above. This time vptr is directed to offset 24.
vptr , ? gcc, , . , RTTI ( ), . .
+6