如何强制执行“覆盖"关键字?
有什么方法可以强制在 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.
相关文章