3

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

:)

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Ming
  • 487
  • 2
  • 7
  • 14

5 Answers5

5

In cases like this, operator precedence tends to get in the way.

What this creates is an alias (named healthCalcFunc) for the type "pointer to a function taking a reference to a const GameCharacter as its parameter and returning an int".

  1. int: return type
  2. (*healthCalcFunc): Pointer to function -- must be in parens to bind the * to the name instead of the preceding int, which would declare a function returning a pointer instead of the desired pointer to a function.
  3. (const GameCharacter &): the parameter list for the type of function this will point at.
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
5
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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

It's a function pointer - hopefully your book will have explained these somewhere; if not, your favourite search engine should be able to point you in the right direction.

Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
0

That is typedef for a function pointer for functions which return int and take const GameCharacter& as an argument.

You can create a function pointer using healthCalcFunc hp = &MyFunc; then use it as int n = (*hp)(GameCharacter());. Here MyFunc will have this signature: int MyFunc(const GameCharecter&);.

Naveen
  • 74,600
  • 47
  • 176
  • 233
0

This is a function type definition. At first glance, it's strange, but you'll get used to it. basically, what it says is, define a type named healthCalcFunc, whose return value is an integer, and takes a constant GameCharacter reference as its only argument.

In general, the form of a function pointer declaration is as follows:

typedef return_type (*new_function_typename)(typeof_arg1, typeof_arg2, ...);

And you can use it like this:

new_function_typename functor;
functor = some_other_functions_name;
// or
functor = dlsym(dlopen_handle, "some_function_name"); // for dynamic loading

int retval = functor(arg1, arg2);