typedef int (*healthCalcFunc) (const GameCharacter&);
It introduces a name called healthCalcFunc for a type which describes a function-pointer, taking one parameter of type const GameCharacter&
and returning an int
.
So this means, if you've a function as:
int some_function(const GameCharacter&)
{
//...
}
Then you can create a pointer-object which would point to the above function as:
healthCalcFunc pfun = some_function;
and then use pfun
in place of some_function
as:
some_function(args); /normal call
pfun(args); //calling using function pointer
//it is exactly same as the previous call
And benefit with this approach is that you can pass around pfun
(or some_function
) to other function as:
void other_function(healthCalcFunc pfun)
{
//..
pfun(args); //invoke the function!
//..
}
healthCalcFunc pfun = some_function;
other_function(some_function);
other_function(pfun);
Here other_function
will use the function pointer to invoke the function. That way, next time you can pass another function matching the function signature to other_function
and other_function
will invoke that another function instead.