views:

605

answers:

9

I'm trying to make a function that takes a character, then returns a pointer to a function depending on what the character was. I just am not sure how to make a function return a pointer to a function.

A: 

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

int returnsOne() {
     return 1;
}

typedef int(*fp)();

fp returnsFPtoReturnsOne() {
    &returnsOne;
}

In your specific case:

fp getFunctionFor(char code) {
    switch (code) {
        case 'a': return &functionA;
        case 'b': return &functionB;
    }
    return NULL;
}
Thomas Jung
My down vote is because he wants to return the function pointer, not cast to void* and return it.
CiscoIPPhone
yes, my first response was quick and dirty, I fixed ... eating crow now.
Thomas Jung
+4  A: 

Create a typedef for the function signature:

typedef void (* FuncSig)(int param);

Then declare your function as returning FuncSig:

FuncSig GetFunction();
sean e
A: 

I'm assuming C here (no objects, but C++ comment style :) :

// 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;
}
Rutger Nijlunsing
+2  A: 
typedef void (*voidFn)();

void foo()
{
}

voidFn goo(char c)
{
    if (c == 'f') {
        return foo;
    }
    else {
        //..
    }
    // ..
}
Magnus Skog
+14  A: 
#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;
}
anon
Very Nice Example
Baget
+1  A: 

Easiest way is to typedef the pointer-to-function type you want, and then use that

typedef void (*fnptr_t)(int, int);
fptr_t myfunc(char *) { ....
Chris Dodd
+1  A: 

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

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

stefaanv
A: 

Something like this

#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;
}
Beano
A: 
int f(char) {
    return 0;
}

int (*return_f())(char) {
    return f;
}

No, seriously, use a typedef :)

erikkallen