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

int f(char) {
    return 0;
}

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

No, seriously, use a typedef :)


Create a typedef for the function signature:

typedef void (* FuncSig)(int param);

Then declare your function as returning FuncSig:

FuncSig GetFunction();

#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;
}