0

I want to make a function call based on the input string given by the user.

Example:

void hello()
{
cout<< "print hello";
}

void hello_world()
{
cout<<"print hello_world";
}

int main()
{
string func_name;
cout<< "enter the function you would like to call: \n 1.Hello \n 2.Hello_world";
cin>> func_name;
func_name();
}
  • 5
    You could have a `std::map` where the function names are the keys and the function pointers the values. If you want to call any arbitrary function, then the answer is no (at least in plain C++). – LHLaurini Apr 20 '23 at 21:42
  • 1
    By default C++ has no reflection capabilities. A major reason you use C++ is for efficiency. Names are inefficient. The computer doesn't need them, it just uses an address, an offset from an address, or a register. Names are just there for the humans writing and maintaining the code, so once the linking phase of the build process is complete and the program has been completely translated from human code into machine code, the names are usually eliminated. – user4581301 Apr 20 '23 at 21:59

0 Answers0