覆盖非虚函数
它所说的 C++11 FDIS
The C++11 FDIS it says
如果一个虚函数被标记了 virt-specifier 覆盖并且没有覆盖它的成员函数一个基类,程序格式不正确.[ 例子:
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
};
如果 B::f
不会被标记为虚拟怎么办? 那么程序是非良构的吗?或者是 override
然后被忽略`.我在标准文本中找不到任何处理这种情况的方法.
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.
更新 1/2(合并)我向 C++ 编辑转发了一个请求以调查事情.感谢 Johannes 向我指出这一点.
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" 不会覆盖函数,尤其是.没有虚拟的,
- 因此它不是虚拟的
- 因此文本如果一个虚函数被标记为..."不适用
- 因此示例与文本不匹配.
但通过意识到这一点,我发现覆盖"上下文关键字的意图不能满足:如果函数名称中的拼写错误或错误的参数类型确实使函数本身非虚拟,那么标准的文本从不适用――并且覆盖"变得无用.
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.
最好的解决办法可能是
- 将虚拟"放在示例函数的前面
推荐答案
如果
B::f
不会被标记为虚拟怎么办?那么程序是不是格式错误的呢?
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.
相关文章