Yes, there is a way to do this.
//Array for the functions std::array<std::function<void()>, 3> functions = { &function1, &function2, &function3 }; //You could also just set them manually functions[0] = &function1; functions[1] = &function2; functions[2] = &function3;
Then you can use it like a regular array:
functions[whichFunction](); //Calls function number 'whichFunction'
Please note that all functions must have the same signature.
If you do not want to use std::function for any reason, you can use function pointers .
Rakete1111
source share