覆盖在非虚拟函数 [英] override on non-virtual functions
问题描述
在C ++ 11 FDIS它说:
如果一个虚函数标有的virt-符覆盖和不覆盖的成员函数
一个基类,是病态的程序。 [示例:结构B {
虚拟无效F(INT);
};
结构D:B {
无效F(长)覆盖; //错误:错误的签名重写B ::˚F
无效F(INT)覆盖; // 好
};
块引用>如果
B ::˚F
就不会被标记虚拟什么?是计划不周形成的,然后呢?或者是覆盖
然后将ignored`。我找不到这种情况下,在性病的任何文本处理。更新1/2 (合并)我转交了对C ++编辑的要求寻找到的东西。谢谢约翰内斯的要指出这一点给我。
- 无效F(长)覆盖不会覆盖功能,ESP。没有虚拟的,
- 因此不虚
- 所以文若一个虚函数标有......并不适用
- 因此,例如不匹配文本。
但是,意识到这一点,我发现,认为越权上下文关键字的意图无法得到满足:如果函数名称的拼写错误或错误的参数类型确实让本身的功能非虚,那么标准的文本永远不会应用 - 与覆盖,也不起作用。
最佳的解决方案可能是
- 将虚拟中的例子的功能前
解决方案
如果
B ::˚F
不会被虚标什么?在程序不当形成的,然后呢?
块引用>是的,是的。因为为了 覆盖的的东西,这东西的具有的是虚的。否则,它不是 覆盖的,它的的隐藏的。所以,肯定的回答从你的问题如下报价
The C++11 FDIS it says
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 };
What if
B::f
would not have been marked virtual? Is the program ill-formed, then? Or isoverride
then to be ignored`. I can not find any handling of this case in the std text.Update 1/2 (merged) I forwarded a request to the C++ Editors to look into things. Thanks Johannes to pointing that out to me.
- "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.
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
解决方案What if
B::f
would not have been marked virtual? Is the program ill-formed, then?Yes, it is. Because in order to override something, that something has to be virtual. Otherwise it's not overriding, it's hiding. So, the positive answer follows from the quote in your question.
这篇关于覆盖在非虚拟函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!