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

  • Home
  • SEARCH
  • 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 6606941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:26:57+00:00 2026-05-25T19:26:57+00:00

I have never see a grammar in c++ like this before: typedef int (callback)(int);

  • 0

I have never see a grammar in c++ like this before:

typedef int (callback)(int);

what really does this really mean?I just find that if I create a statement

  callback a;

It’s effect is very very similar to a forward function declaration.

below is the code I had written

#include<cstdio>

int callbackfunc(int i)
{
    printf("%d\n",i);
    return i*i;
}

// you can also use typedef int (callback)(int) here!
typedef int (*callback)(int);

void func(callback hook)
{
    hook(hook(3));
}

int main()
{
    func(callbackfunc);
    getchar();
        return 0;
}

You can use

typedef int (*callback)(int);//this is very common to use

in this code,but if we change it to

typedef int (callback)(int); //I'm puzzled by this !

this will also get the same result!

and I know typedef int (*callback)(int) and typedef int (callback)(int)
are two completely different stuff.

  • 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-25T19:26:58+00:00Added an answer on May 25, 2026 at 7:26 pm

    Its because of the fact that in the parameter declaration, the function-type is adjusted to become a pointer-to-function-type.

    typedef int type(int); 
    typedef int (*type)(int); 
    

    The first typedef defines a type which is called function-type, while the second typedef defines a type which is called pointer-to-function-type. In the parameter declaration, function-type is adjusted to become a pointer to function type.

    §13.1/3 (C++03) says,

    Parameter declarations that differ only in that one is a function type and the other is a pointer to the same function type are equivalent. That is, the function type is adjusted to become a pointer to function type (8.3.5).

    [Example:
        void h(int());
        void h(int (*)()); // redeclaration of h(int())
        void h(int x()) { } // definition of h(int())
        void h(int (*x)()) { } // ill-formed: redefinition of h(int())
    ]
    

    An interesting example of the exclusive usage of function-type

    Suppose you’ve a typedef, defined as:

    typedef void funtype();
    

    then you can use this to define member-function as:

    struct A
    {
       //member function declaration. 
        funtype f; //equivalent to : void f();
    };
    
    void A::f() //definition
    {
      std::cout << "haha" << std::endl;
    }
    

    Test code:

    int main() {
            A a;
            a.f(); //call member function
    }
    

    Output:

    haha
    

    Online demo: http://ideone.com/hhkeK

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before you answer this I have never developed anything popular enough to attain high
$test= <<<EOF .... EOF; I have never see it before. What's it used for?
I have never really thought about this until today, but after searching the web
Just another of those things that I have never done before but which is
I have never worked with web services and rails, and obviously this is something
I have never seen a way to do this nicely, i would be interested
I have never really found the design view in Visual Studio useful when developing
i have never done embedded (i dont know if thats what you call this)
I always see a single file being used for named query. I have never
I have never really done any serious testing other then writing some simple Java

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.