I’ve only just yesterday figured out function pointers, and I am implementing a console/command system in a game engine.
I figured using a map with a string key and function pointer value would eliminate the need for a huge list of if statements when choosing what to do when running the commands.
I’m getting this error:
argument of type
"void (Game::*)(std::string prop, std::string param)"
is incompatible with parameter of type
"void (*)(std::string prop, std::string param)"
Now I think I know what this means. I could use a static function to get round this, but I want to be able to reference a method of a specific instance of Game.
However the map of function pointers must be able to point to any function with return void and 2 string parameters.
Firstly is this possible?
If not, is it somehow possible to modify instance variables through a static member function? I don’t have high hopes for this.
Any help is appreciated as always.
Function pointers are the suck. Don’t ever use them unless you’re absolutely forced to. Instead, prefer
std::function<void(std::string, std::string)>andstd::bind/lambdas. Unlike function pointers, these can work with any function object, including a bound member function.