class B1 {
virtual void f1();
int int_in_b1;
};
class B2 {
virtual void f2();
int int_in_b2;
};
class D: B1, B2 {
int int_in_d;
void f1();
void f2();
};
class D1: B1, B2 {
int int_in_d;
virtual void f1();
virtual void f2();
};
Based on this article, the memory layout for an object d
of class D
is like this:
d:
+0: pointer to virtual method table of D (for B1)
+4: value of int_in_b1
+8: pointer to virtual method table of D (for B2)
+12: value of int_in_b2
+16: value of int_in_d
virtual method table of D (for B1):
+0: D::f1() // B1::f1() is overridden by D::f1()
virtual method table of D (for B2):
+0: D::f2() // B2::f2() is overridden by D::f2()
What about an object of class D1
? In class D1
, the members f1
and f2
are both declared as virtual
!
The use of
virtual
is redundant inD1
.From C++11, §10.3¶2:
Thus, the memory layout (which is what the question seems to be about) is the same for
D
andD1
. Obviously, different types will have different virtual tables.