I have never see a grammar in c++ like this before:
typedef int (callback)(int);
what really does this really mean?I just find that if I create a statement
callback a;
It’s effect is very very similar to a forward function declaration.
below is the code I had written
#include<cstdio>
int callbackfunc(int i)
{
printf("%d\n",i);
return i*i;
}
// you can also use typedef int (callback)(int) here!
typedef int (*callback)(int);
void func(callback hook)
{
hook(hook(3));
}
int main()
{
func(callbackfunc);
getchar();
return 0;
}
You can use
typedef int (*callback)(int);//this is very common to use
in this code,but if we change it to
typedef int (callback)(int); //I'm puzzled by this !
this will also get the same result!
and I know typedef int (*callback)(int) and typedef int (callback)(int)
are two completely different stuff.
Its because of the fact that in the parameter declaration, the function-type is adjusted to become a pointer-to-function-type.
The first typedef defines a type which is called
function-type, while the second typedef defines a type which is calledpointer-to-function-type. In the parameter declaration, function-type is adjusted to become a pointer to function type.§13.1/3 (C++03) says,
An interesting example of the exclusive usage of function-type
Suppose you’ve a typedef, defined as:
then you can use this to define member-function as:
Test code:
Output:
Online demo: http://ideone.com/hhkeK