How to enforce the #39;override#39; keyword?(如何强制执行“覆盖关键字?)
问题描述
有什么方法可以强制在 Visual C++ 2012 中使用 C++11 override 关键字?
Is there any way to enforce the usage of the C++11 override keyword in Visual C++ 2012?
(即如果我忘记说override,那么我想收到警告/错误.)
(i.e. if I forget to say override, then I want to get a warning/error.)
推荐答案
C++11 几乎有你想要的.
C++11 almost had what you want.
最初 override 关键字是更大提案的一部分 (N2928),其中还包括强制使用的能力:
Originally the override keyword was part of a larger proposal (N2928) which also included the ability to enforce its usage:
class A
{
virtual void f();
};
class B [[base_check]] : public A
{
void f(); // error!
};
class C [[base_check]] : public A
{
void f [[override]] (); // OK
};
base_check 属性会使在不使用 override 关键字的情况下覆盖虚函数时出错.
The base_check attribute would make it an error to override a virtual function without using the override keyword.
还有一个 hiding 属性,表示函数隐藏基类中的函数.如果使用了 base_check 并且一个函数在基类中隐藏了一个函数而不使用 hiding,那就是一个错误.
There was also a hiding attribute which says a function hides functions in the base class. If base_check is used and a function hides one from the base class without using hiding it's an error.
但是大部分提案都被放弃了,只有 final 和 override 特性被保留下来,作为具有特殊含义的标识符"而不是属性.
But most of the proposal was dropped and only the final and override features were kept, as "identifiers with special meaning" rather than attributes.
这篇关于如何强制执行“覆盖"关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何强制执行“覆盖"关键字?
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
