是否建议明确覆盖函数虚拟? [英] Is it recommended to explicitly make overriding functions virtual?

查看:148
本文介绍了是否建议明确覆盖函数虚拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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!

这篇关于是否建议明确覆盖函数虚拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆