为什么下面的类有一个虚表?
假设我有一个钻石继承情况如下:
class A{
public: virtual void foo(){};
};
class B: public virtual A{
public: virtual void foo(){};
};
class C: public virtual A{
public: virtual void foo(){};
};
class D: B, C{};
最后一行产生一个编译错误,引用歧义。据我了解,问题在于编译器不知道将哪个 foo 放入 D 的 vtbl 中,但是如果 D 没有定义自己的任何虚拟函数,为什么会有 D 的 vtbl 呢?
Suppose I have a diamond inheritance situation as follows:
class A{
public: virtual void foo(){};
};
class B: public virtual A{
public: virtual void foo(){};
};
class C: public virtual A{
public: virtual void foo(){};
};
class D: B, C{};
The last line yields a compilation error citing ambiguity. As I understand it, the problem is that the compiler doesn't know which foo to place in D's vtbl, but why would there even be a vtbl for D if it doesn't define any virtual functions of its own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在继承包含虚函数的类。因此,你的类有虚函数。就这么简单。
You're inheriting classes that contain virtual functions. Therefore, your class has virtual functions. It's as simple as that.