Function returning a function

I want to call a function that returns a pointer to the right function depending on what inputChar is. All the functions returned are void and take no parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pointerToVoidFunction Input::getKeyFunction(){
    switch(inputChar){
        //Enter
        case 13:
            return /*pointer to Input::Enter*/;
        //Space
        case 32:
            return /*pointer to Input::Space*/;
        //Backspace
        case 8:
            return /*pointer to Input::Backspace*/;
        default:
            return nullptr;
    }
}


I don't know what to type in the pointerToVoidFunction part or how to return the pointer. I haven't found a clear explanation that I could just implant so if you know anything, please let me know!
Last edited on
The pointer-to-function syntax is a bit unintuitive, particularly when used as the return type of a function. It's easier to just use a typedef:
 
typedef void (*pointerToVoidFunction)();

Without using a typedef (if I'm not mistaken):
 
void (*Input::getKeyFunction())(){ /*...*/ }
Last edited on
Topic archived. No new replies allowed.