I try to save the pointers of Actions class methods in std::map container. When I try to do it compiler shows C3867 and C2440 errors. Here is the screenshot of the errors
#include <iostream>
#include <map>
using namespace std;
typedef void(*FunctionPointer)();
class Actions
{
public:
Actions(){}
~Actions(){}
void DoSomething() { cout << "Something" << endl; }
};
class Commands
{
public:
Commands(){}
~Commands() {}
// Initialize commands list
void Init() { commandsList_["Something"] = actions_.DoSomething; }
// Calls command using its name
void CallCommand(std::string commandName) { commandsList_[commandName](); }
private:
std::map <std::string, FunctionPointer> commandsList_;
Actions actions_;
};
int main()
{
Commands commands;
commands.Init();
commands.CallCommand("Something");
return 0;
}
I tried to cast it: commandsList_["Something"] = (FunctionPointer)actions_.DoSomething;
but it shows error C2440 Screenshot
I have hundreds of commands and want to reduce complexity of main console method and don't want to call every method one by one. What should I do? Is it even possible to save function as pointer?