在C ++ 11之前,当一个虚函数在派生类中被覆盖时,建议在派生类函数中添加虚拟关键字,以使意图清晰。
In times before C++11 when a virtual function was overriden in a derived class, it was recommended to add the virtual keyword also to the derived class function to make the intention clear.
现在,这样的函数被标记为override,其中包括必须有一个虚拟基函数的概念。因此,我现在更喜欢省略虚拟:
Nowadays such a function is marked "override" which kind of includes the notion that there must be a virtual base function. Therefore I am now preferring to omit the virtual:
class Derived: public Base { public: void Overriden() override; // Instead of: virtual void Overriden() override; };<一个带有明确虚拟关键字的虚函数声明
However this leads to an IntelliSense error in MSVC 2012: the 'override' modifier requires a virtual function declaration with an explicit 'virtual' keyword
显然,编译器编译类,但是错误让我想想。
Obviously the compiler compiles the class, but the error makes me think about it. Are there still valid reasons to add the virtual keyword?
推荐答案我认为这更像是一个味道的问题: - )
I think this is more a question of taste :-)
我喜欢只在定义后面写入覆盖,这意味着函数已经是虚拟的。程序员的定义是惰性的,所以保持源代码短: - )
I prefer to only write override behind the definition which implies that the function is already virtual. Programmers are lazy by definition, so keep the source short :-)
我们已经添加一条规则到我们的编码指南,其中覆盖是必须的,虚拟应该从
We had add a rule to our coding guidelines where override is a must and the virtual should be removed from old code during normal change process or while refactoring the actual class.
但这只是我们的解决方案,没有技术上的原因!
But this is only our solution and there is no technical reason for this!
更多推荐
是否建议明确覆盖函数虚拟?
发布评论