I have a function that return a String, and I would like to use this String like a name of another function, but I can't because obviously is a String :(.
For example, the function foo return a String foo2, and foo2 is a name of the function that takes parameters, so
(foo(x))(y z)
I hope that you understand my problem.
Thanks,
Alvaro.-
There is no way to access function names at runtime, they do not exist in the machine code. You have to compile them into the program, for example as a map that associates function names (as strings) with the functions themselves (as functions, or as function pointers, etc)
#include <iostream>
#include <functional>
#include <map>
#include <string>
void foo2(int a, int b)
{
std::cout << a+b << '\n';
}
std::string foo(int x)
{
if(x == 1)
return"foo2";
}
int main()
{
std::map<std::string, std::function<void(int, int)>> m;
m["foo2"] = foo2;
int x = 1;
int y = 2;
int z = 3;
m[foo(x)](y, z);
}