C++ 覆盖非虚函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7530616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
override on non-virtual functions
提问by towi
The C++11 FDIS it says
它说的 C++11 FDIS
If a virtual function is marked with the virt-specifier override and does not override a member function of a base class, the program is ill-formed. [ Example:
struct B { virtual void f(int); }; struct D : B { void f(long) override; // error: wrong signature overriding B::f void f(int) override; // OK };
如果一个虚函数被标记为 virt-specifier override 并且没有重写基类的成员函数,那么程序就是格式错误的。[ 例子:
struct B { virtual void f(int); }; struct D : B { void f(long) override; // error: wrong signature overriding B::f void f(int) override; // OK };
What if B::f
would not have been marked virtual?Is the program ill-formed, then? Or is override
then to be ignored`. I can not find any handling of this case in the std text.
如果B::f
没有被标记为虚拟会怎样?那么该程序格式错误吗?否则override
将被忽略`。我在标准文本中找不到对这种情况的任何处理。
Update 1/2(merged) I forwarded a request to the C++ Editors to look into things. Thanks Johannesto pointing that out to me.
更新 1/2(合并)我向 C++ 编辑转发了一个请求以调查事情。感谢约翰内斯向我指出这一点。
- "void f(long) override" does not override a function, esp. no virtual one,
- therefore it is not virtual
- therefore the text "If a virtual function is marked with..." does not apply
- therefore the example does not match the text.
- “void f(long) override”不会覆盖函数,尤其是。没有虚拟的,
- 因此它不是虚拟的
- 因此文本“如果虚拟函数被标记为...”不适用
- 因此该示例与文本不匹配。
But by realizing this I found, that the intention of the "override" contextual keyword can not be met: if a typo in the function name or the wrong argument type does make the function itself non-virtual, then the standard's text never applies -- and "override" is rendered useless.
但是通过意识到这一点,我发现无法满足“覆盖”上下文关键字的意图:如果函数名称中的拼写错误或错误的参数类型确实使函数本身成为非虚拟函数,则标准的文本永远不会适用 - - 并且“覆盖”变得无用。
The best possible solution may be
最好的解决方案可能是
- putting "virtual" in front of the example's functions
- 将“虚拟”放在示例函数的前面
采纳答案by Armen Tsirunyan
What if
B::f
would not have been marked virtual? Is the program ill-formed, then?
如果
B::f
没有被标记为虚拟会怎样?那么该程序格式错误吗?
Yes, it is. Because in order to overridesomething, that something has tobe virtual. Otherwise it's not overriding, it's hiding. So, the positive answer follows from the quote in your question.
是的。因为为了覆盖某些东西,某些东西必须是虚拟的。否则它不是覆盖,而是隐藏。因此,肯定的答案来自您问题中的引用。
回答by Branko Dimitrijevic
If B:f
was non-virtual, then bothD:f
functions would be ill-formed.
如果B:f
是非虚拟的,那么这两个D:f
函数都是病态的。
回答by deft_code
Yes, the program is ill formed when override
is added to any non-virtual function.
是的,当override
被添加到任何非虚拟函数时,程序格式错误。
Generally, functions with differing signatures (overloaded), are as different as functions with different names. The example given in the Spec is not meant to imply that the function name effects override
. It's meant to show the common error that override
is designed to prevent.
通常,具有不同签名(重载)的函数与具有不同名称的函数一样不同。规范中给出的示例并不意味着函数名称影响override
. 它旨在显示override
旨在防止的常见错误。