1

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. Commented Aug 4, 2019 at 11:24
  • 1
    ->* 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. Commented Aug 4, 2019 at 11:42

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

1 Comment

tnx you very much, in me real code i used shared_ptr so i need to change little bit: {auto p = myMap["place"].get(); (p->*(p->func["run"]))(5);}
3

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.