typedef int (*healthCalcFunc) (const GameCharacter&);
これは、関数ポインターを記述する型のhealthCalcFuncという名前を導入し、型の1つのパラメーターを取り、をconst GameCharacter&
返しますint
。
つまり、次のような関数がある場合です。
int some_function(const GameCharacter&)
{
//...
}
次に、上記の関数を次のように指すポインタオブジェクトを作成できます。
healthCalcFunc pfun = some_function;
次に、asの代わりに使用しますpfun
。some_function
some_function(args); /normal call
pfun(args); //calling using function pointer
//it is exactly same as the previous call
このアプローチの利点は、次のように他の関数に渡すpfun
(または)ことができることです。some_function
void other_function(healthCalcFunc pfun)
{
//..
pfun(args); //invoke the function!
//..
}
healthCalcFunc pfun = some_function;
other_function(some_function);
other_function(pfun);
ここでother_function
は、関数ポインタを使用して関数を呼び出します。そうすれば、次回、関数シグネチャに一致する別の関数を渡すことができ、other_function
代わりother_function
にその別の関数を呼び出すことができます。