Based on post How to call a function by its name (std::string) in C++?, tried to make a version using CLASS, but my approach does not work.
class A { public: int add(int i, int j) { return i+j; } int sub(int i, int j) { return i-j; } }; typedef int (*FnPtr)(int, int); int main(int argc, char* argv[]) { // initialization: std::map<std::string, FnPtr> myMap; A a; myMap["add"] = a.add; myMap["sub"] = a.sub; Returns this erro:
main.cpp:31:22: error: cannot convert ‘A::add’ from type ‘int (A::)(int, int)’ to type ‘std::map<std::basic_string<char>, int (*)(int, int)>::mapped_type {aka int (*)(int, int)}’ main.cpp:32:22: error: cannot convert ‘A::sub’ from type ‘int (A::)(int, int)’ to type ‘std::map<std::basic_string<char>, int (*)(int, int)>::mapped_type {aka int (*)(int, int)}’ Does anyone know what is the error?
myMap["add"](1, 2)"), because that makes no sense. Instead, you need to combine the class memberA::addand the instance objectain some way.typedef int (*FnPtr)(int, int);totypedef int (A::*FnPtr)(int, int);and call it likea.*(myMap["add"])(42, 1337);. Horrible, isn't it?std::function+std::bindclass A?