虚拟关键字在函数声明中的位置 [英] position of virtual keyword in function declaration

查看:83
本文介绍了虚拟关键字在函数声明中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否将virtual关键字放在函数声明中,然后再返回值类型之前还是之后,有什么区别吗?

Does it make any difference whether I place the virtual keyword in a function declaration before or after the return value type?

virtual void DoSomething() = 0;
void virtual DoSomething() = 0;

在重构一些旧代码时发现了void virtual语法,并且想知道它是否正在编译...

Found the void virtual syntax while refactoring some legacy code, and was wondering that it is compiling at all...

推荐答案

这两个语句是等效的.
但是第一个比较传统.因为通常必填字段保持与任何语法(即示例中的函数原型)最接近.

Both the statements are equivalent.
But the 1st one is more conventional. Because, generally mandatory fields are kept closest to any syntax (i.e. the function prototype in your example).

virtual是可选关键字(不过,对于纯virtual而言,它是必需的).但是,返回类型(此处为void)是必填关键字,始终是必需的.因此,人们将virtual放在最左侧,而return类型则更靠近函数签名.

virtual is an optional keyword (it's needed for pure virtual though). However return type (here void) is a mandatory keyword, which is always required. So people keep virtual on the left most side and the return type a little closer to the function signature.

另一个示例:由于相同的原因,我通常在下面的代码中看到第一种语法更受欢迎:

Another example: I generally see that in below code 1st syntax is more popular for the same reason:

const int i = 0;  // 1
int const i = 0;  // 2

这篇关于虚拟关键字在函数声明中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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