Skip to content

Instantly share code, notes, and snippets.

@MATRIXKOO
Created February 23, 2022 13:35
Show Gist options
  • Save MATRIXKOO/f3814906365af7e45e4f240b8a40b0a2 to your computer and use it in GitHub Desktop.
Save MATRIXKOO/f3814906365af7e45e4f240b8a40b0a2 to your computer and use it in GitHub Desktop.
Diamond class.
class X {};
class Y : public virtual X {};
class Z : public virtual X {};
class A : public Y , public Z {};
/*
X --> +-----------+
| 1 bytes |
+-----------+
vtable
+-----------------------+
| 0 (top_offset) |
+-----------------------+
Y/Z-->+----------+ | ptr typeinfo for Y/Z |
| vtptr |-------> +-----------------------+
+----------+
| pad |
+----------+
sizeof(D) = 48,align = 8 vtable
+-----------------------+
| 32 (vbase_offset) |
+-----------------------+
| 0 (top_offset) |
+-----------------------+
| ptr to typeinfo for D |
+----------> +-----------------------+
d --> +----------+ | | B::w() |
| vtptr |----+ +-----------------------+
+----------+ | D::y() |
|pad |b | +-----------------------+
+----------+ | 16 (vbase_offset) |
| vtptr |---------+ +-----------------------+
+----------+ | | -16 (top_offset) |
| d | c | | +-----------------------+
+----------+ | | ptr to typeinfo for D |
| vtptr |----+ +-----> +-----------------------+
+----------+ | | C::x() |
| pad | a | | +-----------------------+
+----------+ | | 0 (vbase_offset) |
| +-----------------------+
| | -32 (top_offset) |
| +-----------------------+
| | ptr to typeinfo for D |
+----------> +-----------------------+
| A::v() |
+-----------------------+
*/
int main()
{
X x;
Y y;
Z z;
A a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment