虚拟功能的语法 [英] Syntax for virtual functions

查看:102
本文介绍了虚拟功能的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中使函数虚拟化时,我必须在哪里写"virtual"关键字?如果我编写虚拟int function_name而不是像Java中那样编写int virtual function_name会有所不同

While making a function virtual in c++ where do I have to write "virtual" keyword? Does it make a difference if I write virtual int function_name instead of int virtual function_name like in java

推荐答案

在c ++中使函数虚拟化时,我必须在哪里写virtual关键字?

在函数声明中,在函数名称之前,任何属性说明符之后以及其他说明符(包括函数的返回类型的类型说明符).

In the function declaration, before the function name, and after any attribute specifiers, along with the other specifiers (including the type specifier for the function's return type).

声明的一般语法是

simple-declaration:
    decl-specifier-seq<opt> init-declarator-list<opt> ;
    attribute-specifier-seq decl-specifier-seq<opt> init-declarator-list ;

其中virtual(一个功能说明符)和返回类型(一个 type-specifier )都是 decl-specifier-seq的一部分,并且声明的名称是 init-declarator-list 的一部分.

where both virtual (a function-specifier) and the return type (a type-specifier) are part of the decl-specifier-seq, and the name being declared is part of the init-declarator-list.

如果我写的是virtual int function_name而不是int virtual function_name

不,说明符的顺序没有区别.

No, the order of the specifiers makes no difference.

但是,如果将virtual放在类型说明符之后,您可能会感到有些惊讶,因为将它放在前面更为常见.直到我回答了这个问题,我才知道你能做到这一点.

However, you might surprise some people if you put virtual after the type specifier, since it's more common to put it before. I didn't know you could do that until I answered this question.

这篇关于虚拟功能的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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