Ming
Ming

Reputation: 497

Who can help me explain the use of typedef in C++?

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

:)

Upvotes: 3

Views: 245

Answers (5)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361302

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.

Upvotes: 5

Jerry Coffin
Jerry Coffin

Reputation: 490058

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.

Upvotes: 5

user529758
user529758

Reputation:

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);

Upvotes: 0

Naveen
Naveen

Reputation: 73433

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&);.

Upvotes: 0

Philip Kendall
Philip Kendall

Reputation: 4314

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.

Upvotes: 0

Related Questions