我尝试在std :: map容器中保存Actions类方法的指针。当我尝试这样做时,编译器显示C3867和C2440错误。 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;
}
我尝试投了它:commandsList_["Something"] = (FunctionPointer)actions_.DoSomething;
但它显示错误C2440 Screenshot
我有数百个命令,希望降低主控制台方法的复杂性,并且不希望逐个调用每个方法。 我该怎么办?是否可以将函数保存为指针?