1
votes

I'm trying to call member function through a map of member function ptr that is a data member of a different class

{    
    class B
    {
    public:
    typedef void (B::*funcp)(int);
    B()
    {
        func.insert(make_pair("run",&B::run));
    }
        void run(int f);
        map<string,funcp>func;
    };

    class A
    {
        public:
        A();
        void subscribe(B* b)
        {
            myMap["b"] = b;
        }
        map<string,B*>myMap;
        void doSome()
        {
             myMap["place"]->func["run"](5);
        }
    };
    int main()
    {
        A a;
        B b;
        a.subscribe(&b);
        a.doSome();
        return 0;
    }
}

but im getting

error: must use ‘.’ or ‘->’ to call pointer-to-member function in ‘((A*)this)->A::myMap.std::map, B*>::operator[](std::basic_string(((const char*)"place"), std::allocator()))->B::func.std::map, void (B::)(int)>::operator[](std::basic_string(((const char)"run"), std::allocator())) (...)’, e.g. ‘(... ->* ((A*)this)->A::myMap.std::map, B*>::operator[](std::basic_string(((const char*)"place"), std::allocator()))->B::func.std::map, void (B::)(int)>::operator[](std::basic_string(((const char)"run"), std::allocator()))) (...)’

i also tryed :

{
    auto p = myMap["place"];
    (p->*func["run"])(5);
}

and thien the error it:

‘func’ was not declared in this scope

2
I suggest you learn about std::function and lambdas (or possibly std::bind), as that will make stuff like this much easier.Some programmer dude
->* is not like ->– the thing on the right is not the name of a member but an expression that is evaluated in the current scope.molbdnilo

2 Answers

3
votes
B* bptr = myMap["place"];
(bptr->*(bptr->func["run"]))(5);

OK tested now, and thanks to Sombrero Chicken for fixing the typos. You don't need all those parens, but it's probably a good idea to leave them in.

The thing you are missing is that you need your B pointer twice. Once to find the map in the other object, and once to call the member function using the member function pointer.

3
votes

In the code you've tried, p is of type B*. In order to access its run method pointer you should write p->func["run"] not p->*func["run"], and finally to invoke that method you should write p->*(p->func["run"]).

void doSome() {
    // pointer to the B object
    B* p = myMap["place"];

    // pointer to the method
    B::funcp method_ptr = p->func["run"];

    // invoke the method on the object
    p->*method_ptr(5);
}