如何强制使用“override”关键字? [英] How to enforce the 'override' keyword?
问题描述
有什么方法强制使用Visual C ++ 2012中的C ++ 11 override
关键字吗?
(如果我忘记说 override
,那么我想得到一个警告/错误。)
C ++ 11几乎有你想要的。
原来 override
关键字是更大提案的一部分( N2928 )还包括实施其使用的能力:
class A
{
virtual void f
};
class B [[base_check]]:public A
{
void f(); //错误!
};
class C [[base_check]]:public A
{
void f [[override]](); // OK
};
base_check
属性会导致错误以覆盖虚拟函数,而不使用 override
关键字。
还有一个隐藏
属性,它说一个函数隐藏基类中的函数。如果使用 base_check
,并且函数从基类中隐藏了一个函数,而不使用 hiding
但是大部分提案被删除,只有最后
和覆盖
功能被保留,作为具有特殊意义的标识符而不是属性。
Is there any way to enforce the usage of the C++11 override
keyword in Visual C++ 2012?
(i.e. if I forget to say override
, then I want to get a warning/error.)
C++11 almost had what you want.
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
};
The base_check
attribute would make it an error to override a virtual function without using the override
keyword.
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.
But most of the proposal was dropped and only the final
and override
features were kept, as "identifiers with special meaning" rather than attributes.
这篇关于如何强制使用“override”关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!