John
John

Reputation: 67

Typedef function pointer in C++

I am trying to understand a code in c++ header.

#define GET_VAL(fn) void fn(int val)
typedef GET_VAL ((*get_val));
struct myStruct
{ 
    get_val getValue;
};

In the source file, the function getValue is called.

getValue(2);

Anyone have any ideas?

Upvotes: 1

Views: 83

Answers (1)

TartanLlama
TartanLlama

Reputation: 65620

The GET_VAL macro substitutes the tokens you pass to it. This:

typedef GET_VAL ((*get_val));

Expands to:

typedef void (*get_val) (int val);

That is a pointer to a function which takes an int and returns nothing. A function pointer of this type is declared in myStruct (which is presumably defined at some point) and called like a regular function.

Upvotes: 8

Related Questions