【发布时间】:2015-08-27 09:53:39
【问题描述】:
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();
};
基于这个article,类D的对象d的内存布局是这样的:
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()
D1 类的对象呢?在D1 类中,成员f1 和f2 都声明为virtual!
【问题讨论】:
-
动态多态的实现是一个实现细节,而不是C++本身的事实。因此,请指定您正在谈论的 ABI。
-
@jxh,错别字。应该是B1和B2
-
为什么 B1::f1() 在 D 中没有被覆盖?
-
@newbie:来自维基百科文章的剪切和粘贴错误,它说明了如果您不覆盖会发生什么。我解决了这个问题。
标签: c++ compiler-construction multiple-inheritance