C++ Map of string and member function pointer

南笙酒味 提交于 2019-12-05 14:35:55

See std::map::insert and std::map for value_type

myMap.insert(std::map<std::string, myFunc>::value_type("test", &Test::TestFunc));

and for operator[]

myMap["test"] = &Test::TestFunc;

You cannot use a pointer to member function without an object. You can use the pointer to member function with an object of type Test

Test t;
myFunc f = myMap["test"];
std::string s = (t.*f)("Hello, world!");

or with a pointer to type Test

Test *p = new Test();
myFunc f = myMap["test"];
std::string s = (p->*f)("Hello, world!");

See also C++ FAQ - Pointers to member functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!