【发布时间】:2019-10-23 14:11:33
【问题描述】:
以下代码来自MSDN,我复制了这里的代码以避免自己做示例,基本上我的项目中遇到了这个问题。
struct V {
virtual void vf();
};
struct A : virtual V {
void vf() override;
};
struct B : virtual V {
void vf() override;
};
struct D : A, B {
// Uncomment the following line to resolve.
// void vf() override;
};
我查看了这个SO Question,其中建议使用using 指令来解决不明确的符号。
该建议不适用于此示例,而是要解决我们应该显式覆盖派生类中的基方法的问题。
我想避免在派生类中显式覆盖,所以问题是,为什么不能使用上面 SO 链接中建议的 using 指令解决这个问题,而不是覆盖?例如:
struct D : A, B {
using A::vf(); // not going to work
};
避免显式覆盖的原因是在其中一个基类中使用方法代码。
【问题讨论】:
标签: c++ inheritance