13

I'm reading some material about function pointer in C++, and come across one function definition which I do not understand.
Standard function definition have the form:

type name (param...) 

But the following definition seems a little strange to me. Can anyone explain it to me ? Thanks.

float (*GetPtr1(const char opCode)) (float, float)<br> { if(opCode == '+') return &Plus; else return &Minus; // default if invalid operator was passed } 


Note: Plus and Minus are two functions with param (float, float) and return a float.

2
  • it may be better to check 'else if(opCode == '-')' to avoid burning some midnight oil in future Commented Nov 15, 2010 at 14:38
  • Topic doesn't tell much about your question.. It should. Commented Nov 19, 2010 at 12:04

6 Answers 6

16

GetPtr1 is a function that takes an opcode char and returns a pointer to a function. The function it returns takes two floats and returns a float.

A lot of times it's easier to read if you do something like this:

typedef float (*FloatOperationFuncPtr) (float, float); FloatOperationFuncPtr GetPtr1(const char opCode) { if(opCode == '+') return &Plus; else return &Minus; // default if invalid operator was passed } 
Sign up to request clarification or add additional context in comments.

1 Comment

+1, Much easier to use proper typedefs, you could also add the C++0x version, something along the lines of: typedef std::function<float(float,float)> FloatOperationType; if my memory serves me right.
10

The rule for reading hairy declarations is to start with the leftmost identifier and work your way out, remembering that () and [] bind before * (i.e., *a[] is an array of pointers, (*a)[] is a pointer to an array, *f() is a function returning a pointer, and (*f)() is a pointer to a function):

 GetPtr1 -- GetPtr1 GetPtr1( ) -- is a function GetPtr1( opCode) -- taking a single parameter named opCode GetPtr1(const char opCode) -- of type const char *GetPtr1(const char opCode) -- and returning a pointer (*GetPtr1(const char opCode)) ( ) -- to a function (*GetPtr1(const char opCode)) (float, float) -- taking two parameters of type float float (*GetPtr1(const char opCode)) (float, float) -- and returning float 

So, if opCode is equal to '+', GetPtr1 will return a pointer to the function Plus, and if it's '-', it will return a pointer to the function Minus.

C and C++ declaration syntax is expression-centric (much as Bjarne would like to pretend otherwise); the form of the declaration should match the form of the expression as it would be used in the code.

If we have a function f that returns a pointer to int and we want to access the value being pointed to, we execute the function and dereference the result:

x = *f(); 

The type of the expression *f() is int, so the declaration/definition for the function is

int *f() { ... } 

Now suppose we have a function f1 that returns a pointer to the function f defined above, and we want to access that integer value by calling f1. We need to call f1, derefence the result (which is the function f), and execute it, and then dereference that result (since f returns a pointer):

x = *(*f1())(); // *f1() == f, so (*f1())() == f() and *(*f1())() == *f() 

The type of the expression *(*f1())() is int, so the decaration/definition for f1 needs to be

int *(*f1())() { return f; } 

2 Comments

appreciate the in depth explanation of Right-Left Rule +1
+1 for expanding out a function pointer in to it's components. Will help people coming across them for the first time.
5

Always nice to know about http://cdecl.org for such situations. Be aware that it only works if you remove the parameter names. This is what you get for float(*GetPtr1(const char ))(float, float):

declare GetPtr1 as function (const char) returning pointer to function (float, float) returning float

Comments

3

It's a function that takes a const char and returns a pointer to a function that takes float, float and returns a float.

Comments

2

That means a function which takes a character and returns a pointer to a function that takes two floats and returns a float.

Comments

1

GetPtr1 is a function that takes two float as input parameters and returns a pointer to a function. This is much more clear:

typedef float(*Func)(float, float); Func GetPtr1(const char opCode) { if(opCode == '+') return &Plus; else return &Minus; // default if invalid operator was passed } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.