Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3357224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:36:27+00:00 2026-05-18T02:36:27+00:00

I’m reading some material about function pointer in C++, and come across one function

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:36:28+00:00Added an answer on May 18, 2026 at 2:36 am

    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; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.