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;
Found the void virtual
syntax while refactoring some legacy code, and was wondering that it is compiling at all...
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
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 purevirtual
though). However return type (herevoid
) is a mandatory keyword, which is always required. So people keepvirtual
on the left most side and thereturn
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:
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
And further
function-specifier
are explained in,7.1.2 Function specifiers
Function-specifiers can be used only in function declarations.