How to call a function by its name (std::string) in C++?
问题 I wonder if there is a simple way to call a function from a string. I know a simple way, using \'if\' and \'else\'. int function_1(int i, int j) { return i*j; } int function_2(int i, int j) { return i/j; } ... ... ... int function_N(int i, int j) { return i+j; } int main(int argc, char* argv[]) { int i = 4, j = 2; string function = \"function_2\"; cout << callFunction(i, j, function) << endl; return 0; } This is the basic approach int callFunction(int i, int j, string function) { if(function