The memory layout of a struct/class in cpp¶
1. without virtual function¶
- Static in global variable state
- Other order located in the struct
2. with virtual function¶
1. Self owning virtual function¶
This will place a vtable_ptr
at the start of this struct. which type is void **
.
And it is pointing to the real and unique virtual function table: A array of void* (function ptr)
2. Inherited from a struct with virtual function¶
vtable_ptr
holding by the parent. And the child's virtual functions will push_back to the table that parent owing. But parent will only know the size of function of itself knows.
The child call its virtual function, can get the parent's vtable_ptr
, then do a offset binary operation to update it.
3. Inherited from 2 struct with virtual functions¶
1. Both A and B have virtual functions¶
C++ | |
---|---|
1 2 3 4 5 6 7 |
|
f_c
function will be push_back to A's vtable
2. A have virtual functions , but B dose not have¶
same as the condition above...
3. A does not virtual functions , but B have¶
C++ | |
---|---|
1 2 3 4 5 6 7 |
|
f_c
function will be push_back to B's vtable..
BUT the B's vtable will be put at the start of this struct.