Joh*_*tte 1 c++ overriding compiler-errors compiler-warnings
也许是个愚蠢的问题.
假设我有以下内容:
class A{
int x;
int y;
virtual int get_thing(){return x;}
};
class B : public A {
int get_think(){return y;}
};
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,B :: get_thing返回x,因为重写代码有拼写错误.
如何在编译时确保在类B中覆盖了get_thing函数,以便它返回y?
假设A::get_thing是虚拟的,并且假设class B是派生的class A,并且您具有C++ 11支持,则可以使用override特殊标识符:
class B : public A{
int get_think() override {return y;}
};
Run Code Online (Sandbox Code Playgroud)
这会产生编译器错误.请注意,这是基于方法的签名,即其名称,cv限定符和参数类型.函数的返回类型或正文不会进入它.
| 归档时间: |
|
| 查看次数: |
160 次 |
| 最近记录: |