如何重写非虚函数?
override
的全新语法允许编译器在没有真正重写虚拟函数N3206。
class Base {
virtual void vfunc();
void afunc();
};
以下情况将是 class Derived : public Base
中的错误,如 Std 示例中所述:
void vfunk() override; // 错误:拼写错误
void vfunc(int) override; // 错误:参数
void vfunc() const override; // err: cv
但是如果基本方法不是虚拟怎么办?
- void afunk() 覆盖; // ?
void afunc(int) override; // ?
void afunc() const override // ?;
The very new syntax of override
allows to let the compiler to report an error, if one does not really override a virtual function N3206.
class Base {
virtual void vfunc();
void afunc();
};
The following cases will be an error in class Derived : public Base
, as mentioned in the Std examples:
void vfunk() override; // err: typo
void vfunc(int) override; // err: argument
void vfunc() const override; // err: cv
But what if the base method is not virtual?
void afunk() override; // ?
void afunc(int) override; // ?
void afunc() const override // ?;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
规范草案(n3242)说
由于您显示的函数声明不是虚拟的,因此您也会遇到冲突
请注意,与基函数具有相同名称和参数列表(包括常量性)但不是虚拟函数的函数不会覆盖该基函数。相反,它是隐藏基本函数。
通过在函数声明是 C++0x 草案的一部分但不会成为 C+ 的一部分之后放置
new
而不是override
来指定函数隐藏基函数+0x,因为在寻找非函数成员的语法点以及时放置new
时存在问题。因此,它被投票给 C++0x。The spec draft (n3242) says
Since the function declarations you show are not virtual, you also run afoul of
Note that a function that has the same name and parameter list (including constness) as a base function, but that is not virtual does not override that base function. It is instead said to hide the base function.
Designating that a function hides a base function by putting
new
instead ofoverride
after the function's declaration was part of the C++0x draft, but will not be part of C++0x as there were problems with finding syntax spots for non-function members for puttingnew
at, in time. Consequently, it was voted out for C++0x.