我有一个抽象的基类,并希望在派生类中实现一个函数.为什么我必须再次在派生类中声明该函数?
class base {
public:
virtual int foo(int) const = 0;
};
class derived : public base {
public:
int foo(int) const; // Why is this required?
};
int derived::foo(int val) const { return 2*val; }
Run Code Online (Sandbox Code Playgroud)
请考虑派生类定义可能位于标头中,而其实现可能位于源文件中.标题通常包含在多个位置("翻译单元")中,每个位置将独立编译.如果您没有声明覆盖,那么编译器就不会在任何其他翻译单元中知道它.
| 归档时间: |
|
| 查看次数: |
302 次 |
| 最近记录: |