2

I currently have a function called IoTHubDeviceClient_LL_SendEventAsync the 3rd parameter of this method takes in a function pointer and its definition looks like this

typedef void(*IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK)(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback);

Now I can use the IoTHubDeviceClient_LL_SendEventAsync function properly if I define a static function as shown in the class below and not making it an instance of the class

class Foo
{
   void test()
   {
      IoTHubDeviceClient_LL_SendEventAsync(DHandle, Mhandle, MyCallback, Null);
   }
}
static void MyCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{.....}

My question is how can I pass in an instance method instead of a static method. I would like to do something like this

class Foo
{
   void test()
   {
      IoTHubDeviceClient_LL_SendEventAsync(DHandle, Mhandle, MyCallback, Null); //<---3rd parameter fails
   }
   void MyCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
   {.....}
}

Any suggestions ?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • 1
    You can not do this without changing the callback signature. In-class member functions differ from standalone functions and have an implicit `this` parameter. See here for member function as callback - https://stackoverflow.com/questions/400257/how-can-i-pass-a-class-member-function-as-a-callback . But in any case, it's either this or that – SomeWittyUsername Nov 14 '20 at 11:55
  • please do not tag unrelated languages. What you are asking for is obviously not possible in C. – 463035818_is_not_an_ai Nov 14 '20 at 12:06
  • don't get confused by the duplicate asking for "free functions", there is no difference between free functions and static methods when passed via pointer, but pointer to non-static methods are different – 463035818_is_not_an_ai Nov 14 '20 at 12:09
  • 1
    @idclev463035818: "wrong" dup, callback has a user data to allow the capture. Searching a correct dup. – Jarod42 Nov 14 '20 at 12:10

0 Answers0