1

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?

2
  • 3
    This is a horrible way to declare a function pointer. I don't see a reason to use the macro like that other than obfuscation. Commented Mar 11, 2015 at 15:54
  • It is a piece of code in a big project. I just gave a simplified version. Maybe they have their own consideration. Anyway you are right, it did obfuscate me. Commented Mar 12, 2015 at 13:18

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tartan. Solved my confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.