Calling a Function From a String With the Function’s Name in C++

c#, c++

Solution

Create a std::map made of strings and function pointers. Create the map with all of the functions that you will want to call.

There are other ways to do it, involving symbol tables and dynamic loaders but those ways are not portable or friendly.

Problem

How can I call a C++ function from a string? Instead of doing this, call the method straight from string: ``` void callfunction(const char* callthis, int []params) { if (callthis == "callA") { callA(); } else if (callthis == "callB") { callB(params[0], params[1]); } else if (callthis == "callC") { callC(params[0]); } } ``` In C# we'd use typeof() and then get the method info and call from there... anything we can use in C++?

Original source

Related problems