How to force a function to return a pointer to a function? (C ++)

I am trying to make a function that takes a character and then returns a pointer to a function depending on what the character was. I just don't know how to get a function to return a pointer to a function.

+61
c ++ function-pointers return-value
Jun 15 '09 at 19:09
source share
15 answers
#include <iostream> using namespace std; int f1() { return 1; } int f2() { return 2; } typedef int (*fptr)(); fptr f( char c ) { if ( c == '1' ) { return f1; } else { return f2; } } int main() { char c = '1'; fptr fp = f( c ); cout << fp() << endl; } 
+66
Jun 15 '09 at 19:15
source share
 int f(char) { return 0; } int (*return_f())(char) { return f; } 

No, seriously, use typedef :)

+93
Jun 15 '09 at 19:26
source share

Create a typedef for the function signature:

 typedef void (* FuncSig)(int param); 

Then declare your function as returning FuncSig:

 FuncSig GetFunction(); 
+17
Jun 15 '09 at 19:15
source share
 typedef void (*voidFn)(); void foo() { } voidFn goo(char c) { if (c == 'f') { return foo; } else { //.. } // .. } 
+4
Jun 15 '09 at 19:15
source share

Here's how to do it without using typedef:

 int c(){ return 0; } int (* foo (void))(){ //compiles return c; } 
+4
Oct 13 '13 at 9:41
source share

The syntax for returning a function is:

 return_type_of_returning_function (*function_name_which_returns_function)(actual_function_parameters) (returning_function_parameters) 

For example: Consider a function that should be returned as follows:

 void* (iNeedToBeReturend)(double iNeedToBeReturend_par) { } 

Now iNeedToBeReturend function can be returned as

 void* (*iAmGoingToReturn(int iAmGoingToReturn_par))(double) { return iNeedToBeReturend; } 

I felt very bad to learn this concept after three years of professional life as a programmer.

A bonus for you, waiting for a pointer to the dereference function.

C ++ Interview Questions

An example of a function returning a function pointer, dlopen in a dynamic library in C ++

+4
Aug 28 '16 at 11:11
source share

In C ++ 11, return types can be used to simplify the syntax. assuming function:

 int c(int d) { return d * 2; } 

This can be returned from a function (which takes a double to show this):

 int (*foo(double e))(int) { e; return c; } 

Using the return type of return, it becomes a little easier to read:

 auto foo2(double e) -> int(*)(int) { e; return c; } 
+4
Mar 08 '17 at 2:38 on
source share

Check out this site - http://cdecl.org

Helps you convert English to C declarations and vice versa!

Cool stuff!

This link decodes the example in erikallen's answer. int (* return_f ()) (char)

+2
02 Sep '13 at 1:38 on
source share

This is a code showing the return of a function pointer. You must first define a "function signature" to return:

 int returnsOne() { return 1; } typedef int(*fp)(); fp returnsFPtoReturnsOne() { &returnsOne; } 

In your particular case:

 fp getFunctionFor(char code) { switch (code) { case 'a': return &functionA; case 'b': return &functionB; } return NULL; } 
+1
Jun 15 '09 at 19:13
source share

The easiest way is to print the required type of function pointer, and then use

 typedef void (*fnptr_t)(int, int); fptr_t myfunc(char *) { .... 
+1
Jun 15 '09 at 19:16
source share

I prefer to return objects and call the operator (). That way, your function can return an interface, and all classes can inherit from this. That is, if you use C ++, not C.

Then you can use the parameterized factor method to return objects based on your input.

+1
Jun 15 '09 at 19:17
source share

Something like that

 #include <iostream> typedef char (*fn_ptr_t)(char); char a_fn(char c) { return c + 1; } char b_fn(char c) { return c + 2; } fn_ptr_t return_function(char c) { fn_ptr_t result = 0; switch (c) { case 'a': result = a_fn; break; case 'b': result = b_fn; break; } return result; } int main() { fn_ptr_t fn = return_function('a'); std::cout << "a(l) = " << (fn)('l') << std::endl; return 0; } 
0
Jun 15 '09 at 19:21
source share

I think it will be useful if someone tells me what happened with the following code (I'm trying to return a pointer to a child method)

 typedef int (Fred::*FredMemFn)(char x, float y); // Please do this! typedef int (B::*FunctFactory)(char x, float y); //function factory class Fred { public: int f(char x, float y) { return 0; } int g(char x, float y) { printf("ggg called "); return 0; } int h(char x, float y) { return 0; } int i(char x, float y) { return 0; } virtual FunctFactory myFunctionFactory(int n) = 0; // ^^ here smth is wrong private: }; class B : public Fred { public: int secreetMethod(char x, float y) { return -100; } FunctFactory myFunctionFactory(int n) { printf("Got parameter %d", n); FunctFactory funct = &B::secreetMethod; return funct; } }; 
0
Oct 17 '16 at 9:55
source share

Assuming int f(char) and ret_f which returns &f .

C ++ 98 / C ++ 03 compatible methods:

  • The Ugly Way:

     int (*ret_f()) (char) { return &f; } 
  • With typedef

    typedef int (sig) (char);

    sig * ret_f () {return & f;

or

 typedef int (*sig_ptr)(char); sig_ptr ret_f() { return &f; } 

Starting with C ++ 11, in addition, we have:

  • final return type:

     auto ret_f() -> int(*)(char) { return &f; } 

    or

     auto ret_f() -> decltype(&f) { return &f; } 
  • typedef with using :

     using sig = int(char); sig* ret_f() { return &f; } 

    or

     using sig_ptr = int (*)(char); sig_ptr ret_f() { return &f; } 

C ++ 14 adds:

  • auto hold:

     auto ret_f() { return &f; } 
0
Apr 18 '19 at 9:53 on
source share

I assume C is here (no objects) :):

 // Type of function which takes a char and returns an int: typedef int (*Func)(char a); // An example of the function you're trying to return and which does something // with char: int exampleFunc(char a) { return (int)(a + 42); } // The function returning the pointer to a function: Func *returnAfunc(void) { return exampleFunc; } 
-one
Jun 15 '09 at 19:15
source share



All Articles