Why parentheses are important in function pointer declaration?
I don't understand why the declaration below is accepted:
typedef void (*_tStandardDeclaration)(LPVOID);
while the following doesn't:
typedef void *_tDeclarationWithoutParenthesis(LPVOID);
typedef void* _tAlternateDeclaration(LPVOID);
开发者_高级运维
I am using MSVC6 (I know it's obsolete and non-standard, but it's needed to maintain a yearly tenth-million revenue system :/ )
The pointer symbol binds to the type by default, so the function pointer needs the parenthesis to indicate that the pointer is actually on the name and not on the return type.
Without the parentheses, you're declaring a function returning a void*
, not a pointer to a function returning void
.
The code below is accepted without a witter by GCC 4.2.1 on MacOS X 10.6.5 with the compiler set to fussy:
c++ -Wall -Wextra -c xx.cpp
Code:
typedef void *LPVOID;
typedef void (*_tStandardDeclaration)(LPVOID);
typedef void *_tDeclarationWithoutParenthesis(LPVOID);
typedef void* _tAlternateDeclaration(LPVOID);
The first gives a pointer to a function returning void
; the latter two are equivalent (spacing makes no difference) and give you a type that is 'function (taking LPVOID argument) that returns pointer to void'.
You can use them to declare function pointers:
typedef _tDeclarationWithoutParenthesis *_tFunctionPointer;
Fun, isn't it...
精彩评论