mardi 2 mai 2017

Pass non-static member function as a callback

I found several similar questions, but the solutions didn't suit my case. In a C++ method, I call a C api, which takes a callback as one of its parameter.

class A
{

   herr_t methodA(some parameters) {....}

   void methodB(some parameters)
   {
       ....

       int status =  CAPI(other parameters, callback, last parameter);
   }

};

The prototype of CAPI is

herr_t CAPI( some parameters, H5L_iterate_t op, other parameters);

H5L_iterate_t is defined by

herr_t (*H5L_iterate_t)( hid_t g_id, const char *name, 
                         const H5L_info_t *info, void *op_data) 

methodA has the same signature as H5L_iterate_t.
In methodB,

status = CAPI(..., **(H5L_iterate_t )std::bind(&A::methodA, this,
              std::placeholders::_1)**, ...);

The compile error I got was "Can't convert from ... to H5L_iterate_t". I'm wondering what's the right way to pass the non static member function as a callback.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire