Cast pointer to member function to normal pointer
Solution 1:
You can't call the member function directly. Member function pointers are not the same type as function pointers.
You'll need to wrap it in a compatible function somehow. However, if your outer function (the one taking the function pointer as an argument) is not re-entrant and does not supply an extra argument for use by the function pointer, you won't be able to pass the instance upon which the member function operates, so you won't actually be able to make the call.
Solution 2:
The difference between a C function and a C++ member function is that C function uses cdecl calling convention, while member functions uses thiscall calling convention (and you can't even take their address!).
As I understand, you actually want that secondFunc()
to call the member function of a particular instance of class (let's call it this
). Well, addresses of member functions of all the instances of a particular class are the same. In order to pass the pointer to the object, you will need a side channel. In this case it could be static variable. Or, if you want MT support, you'll have to use Thread Local Storage (TLS),
This requires one callback per SomeFunc
-type member, but you would need a dispatcher somewhere anyway.
Solution 3:
There is a round about way of doing it. Since C++ names are mangled you can't directly use it for non-static functions.. however since non-static functions have the same signatures as C functions, you can directly use them as callbacks. So for non-static functions, you can have static function wrappers. This page explains this approach in detail.