Search code examples
c++callbackasynccallbackmember-pointers

Passing a pointer to member function


There are a number of examples out there but yet I can't seem to figure out the solution to my problem. I have

class FooSource{
    ...

    void StartGetFoos(void (*callback)(vector<IFoo*>*, IAsyncResult));
    ...
}

When StartGetFoos() is called, a request it done to get Foos, saving the callback. when the request it complete (takes about 30 secs), the saved callback is called with results. I cannot change the signature of this method.

and somewhere else I have a class

class FooUser {
    ...

    void FooUser::MyCallback(vector<IFoo*>* foos, IAsyncResult result) 
    {
          // marshall to UI thread and update UI
    }

    void init()
    {
         fooUser->StartGetFoos(??????);
         // how do I pass my callback member function here?
    }
}

Solution

  • There's nowhere on the signature void (*callback)(vector<IFoo*>*, IAsyncResult) for you to receive a this pointer, so you can't use a member function. Instead you'll have to use a static member function:

    class FooUser {
        static void MyCallback(vector<IFoo*>* foos, IAsyncResult result) 
        {
              // marshall to UI thread and update UI
        }
    
        void init()
        {
             fooUser->StartGetFoos(&FooUser::MyCallback);
        }
    };
    

    The issue here is that you won't be able to access any instance data on FooUser; this may or may not be a problem.

    Depending on how well the API is designed, there may be a way to pass an instance pointer e.g. via IAsyncResult result.