Recently seen question answer: Returning pointer to function syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ampodayas
    New Member
    • Aug 2008
    • 1

    Recently seen question answer: Returning pointer to function syntax

    I've seen alot of confusing explanations for how to return a function pointer without a typedef, so here it is in plain english:

    (Return of func pointer being returned) ((class name)::(functio n to call name)(function to call args)(func pointer to return args){
    }

    (Note: the class anme ins't needed if it isn't in a class, it's just there for completeness.)

    the "function to call name" is not the name of the pointer, but the name of the function that is returning the pointer
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I am not very clear with the question...
    Are you asking how to return a funtion pointer from a function without typedef?

    Following is a sample code to returna function pointer
    [code=c]
    typedef int (*FUN)(int);
    int abc(int x)
    {
    return 0;
    }
    FUN fun_ret_funptr( void)
    {
    return abc;
    }
    [/code]

    Raghu

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You cannot return a function pointer from a function.

      Functions have a return type. That means you have to return a type or a pointer to a type.

      Therefore, you have to typedef your function pointer so it becomes a type. Then you can return it as a pointer to a type.

      Comment

      Working...