How to use a C++ member function as the callback function for a C framework

Viewed 8564

There is a C library (which I cannot change) that supports a callback function of the type

void (*callback)(void *appContext, int eventid)

I want to set a C++ function as the callback.

Specifically I have following questions?

  1. Do I need to declare the callback function under "extern C" block?

  2. Does a member function need to be static to be the callback function? Is it possible to use a non-static member function? If yes, how? And when is it recommended to use a non-static member function?

  3. Does it matter if the function is a template function or not?

  4. Does a non-class C style function have any advantages over a class member function?

I am trying these variants on a old VC++ compiler, which does not support the latest C++ standard. But the code needs to be platform independent and should work on most C++ compilers. I want to know what is recommended practice with callbacks?

6 Answers
Related