c++ - map string to function -
i'm trying not work error:
must use ‘.’ or ‘->’ call pointer-to-member function in ‘((test*)this)->test::mapping.std::map<_key, _tp, _compare, _alloc>::operator[], void (test::*)(int), std::less >, std::allocator, void (test::*)(int)> > >(((const key_type)(& str))) (...)’, e.g. ‘(... ->* ((test*)this)->test::mapping.std::map<_key, _tp, _compare, _alloc>::operator[], void (test::*)(int), std::less >, std::allocator, void (test::*)(int)> > >(((const key_type)(& str)))) (...)’
test.h:  #include.... using namespace std; class test {     public:        std::map<std::string, void(test::*)(int)> mapping;     void add(int); };  test.cpp:  test::test() {    mapping["add"]=&test::add;    string str = "add";    this.*mapping[str](3);// dosnt work!!!! }  void test::add(int a) {    cout<<a<<endl; }   dosnt work, please me.
as this pointer, need use ->* instead of .*. additionally, parentheses needed.
(this->*mapping[str])(3);      
Comments
Post a Comment