position of virtual keyword in function declaration

◇◆丶佛笑我妖孽 提交于 2019-12-01 22:40:34

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

There is no difference between the two, C++ grammar allows virtual keyword to appear both before and after return type. It's just common practice to place it first in the declaration.

Both the formats work but the standard specifys the first format.

Reference:
C++03 7.1 Specifiers

The specifiers that can be used in a declaration are

   decl-specifier:
         storage-class-specifier
         type-specifier
         function-specifier
         friend
         typedef

     decl-specifier-seq:
           decl-specifier-seqopt decl-specifier

And further function-specifier are explained in,

7.1.2 Function specifiers

Function-specifiers can be used only in function declarations.

 function-specifier:
     inline
     virtual
     explicit

tested just now:

compiles both ways.

usualy virtual is put before return type.

read more here: http://msdn.microsoft.com/en-us/library/0y01k918%28v=vs.80%29.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!