I am so confused with this code in the book :
typedef int (*healthCalcFunc) (const GameCharacter&)
and I understand that
typedef double* PDouble, means the word PDouble can be used to declare a pointer to double.
But I can’t figure out the meaning of typedef int (*healthCalcFunc) (const GameCharacter&)
Is there anyone can help me to explain this?
Thanks in advance
🙂
It introduces a name called healthCalcFunc for a type which describes a function-pointer, taking one parameter of type
const GameCharacter&and returning anint.So this means, if you’ve a function as:
Then you can create a pointer-object which would point to the above function as:
and then use
pfunin place ofsome_functionas:And benefit with this approach is that you can pass around
pfun(orsome_function) to other function as:Here
other_functionwill use the function pointer to invoke the function. That way, next time you can pass another function matching the function signature toother_functionandother_functionwill invoke that another function instead.