【发布时间】:2017-01-12 04:05:38
【问题描述】:
我有以下类层次结构。
class A {
public:
virtual bool foo() const;
};
class B : public A {
// Redeclare foo as virtual here?
};
class C : public B {
bool foo() const {/*Definition*/ return true;}
};
class D : public B {
bool foo() const {/*Definition*/ return false;}
};
所以类 C 和 D 想要实现的 foo() 方法,B 没有。我怎样才能做到这一点?我是否必须在 B 类中将 foo() 重新声明为虚拟?
注意:忽略这里和那里的小语法错误。这不是实际代码。我的问题只是关于概念。
【问题讨论】:
-
您是否忘记了实际代码中类声明后的分号?这会导致问题。
-
忽略语法错误。这不是实际的代码。我的问题是关于这个概念的。
标签: inheritance virtual derived