
Passing C++ Class Member Function to as a C Callback Function Parameter
Quote:
> Hello,
> I have a question about passing a C++ Class Member Function as a
> Callback Parameter.
> I am having compiler errors when I pass a C++ Class Member function to a
> function which has a parameter as a Callback function.
> I get the reinterpret cast compile error: can't cast __stdcall to __cdecl*
> Is there any way around this or is it just not possible?
> Thanks,
> JD
It's just not possible, and there are ways around it :) To fix the immediate
problem declare the function as static in the header file. Then you can use it
as a callback.
Then you get the problem that a static function cannot access nonstatic class
members. If you need to do that you need to get your 'this' pointer accessible
to the static function somehow. Many API functions that take a callback also
will take a user-defined parameter that is passed to the callback. Pass
'this'. In the static function cast it and call members....
void CCCClass::staticcallback( void* user )
{
CCCClass* p = (CCCClass*)user;
p->AndAwayWeGo();
Quote:
}
You can also make 'this' accesible by writing it to a static or global variable.
--
Scott McPhillips [VC++ MVP]