cheng
cheng

Reputation: 2146

One question about function definition in C++

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.

Upvotes: 13

Views: 447

Answers (6)

John Bode
John Bode

Reputation: 123598

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

Upvotes: 10

Simone
Simone

Reputation: 11797

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
}

Upvotes: 1

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76886

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

Upvotes: 5

Mark Byers
Mark Byers

Reputation: 839194

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

Upvotes: 2

Lou Franco
Lou Franco

Reputation: 89242

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
}

Upvotes: 16

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

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

Upvotes: 3

Related Questions