是否建议明确覆盖函数虚拟?

编程入门 行业动态 更新时间:2024-10-08 08:24:09
本文介绍了是否建议明确覆盖函数虚拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

是否建议明确覆盖函数虚拟?

本文发布于:2023-10-23 02:40:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   建议

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!