Problem: I have to call functions with its callee names in class. I tried function-pointer with map by return the pointer of functions in class, but it seemed that compiler doesn't allow me to do that with the error
'&': illegal operation on bound member function expression
I want to create a class with function bind with tis callee name ,so I run a register function in its constructor function, then I want to run the funcion inside the class with its callee name. I simplify the code with below,
#include <string>
#include <unordered_map>
class MyClass
{
typedef bool(*fun)(int num);
public:
MyClass() { MyClass_functionReg(); } // not sure if I can do this
~MyClass() {}
bool exec(std::string command)
{
fun function = find(command);
function(num);
return true;
}
private:
std::unordered_map<std::string, fun> v_map;
int num;
private:
bool MyClass_functionReg()
{
v_map.emplace("A", &a); // error here
v_map.emplace("B", &b);
v_map.emplace("C", &c);
v_map.emplace("ERROR", &c);
return true;
}
fun find(std::string command)
{
if (v_map.find(command) != v_map.end())
{
return v_map[command];
}
else
{
return v_map["ERROR"];
}
}
bool a(int num)
{
return true;
}
bool b(int num)
{
return true;
}
bool c(int num)
{
return true;
}
bool error(int num)
{
return true;
}
};
int main(int argc, char** argv)
{
MyClass t;
t.exec("A");
t.exec("B");
return true;
}
Is it just a syntax error? How can i fix this? Is there anything wrong with the way I implement this functionality?