override on non-virtual functions(覆盖非虚函数)
问题描述
它所说的 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.
这篇关于覆盖非虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:覆盖非虚函数


基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01