Passing C++ Class Member Function to as a C Callback Function Parameter 
Author Message
 Passing C++ Class Member Function to as a C Callback Function Parameter

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



Sun, 08 Jun 2003 11:58:32 GMT  
 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]


Sun, 08 Jun 2003 12:16:04 GMT  
 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?

It's possible only if the member function is static.
_______

"Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.

Josh Sebastian



Sun, 08 Jun 2003 12:42:58 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to use member function of a C++ class as a callback function

2. class member function as callback function

3. Calling C++ member function through C function callback

4. Member function callbacks as template parameters

5. passing derived-CDialog object member function as callback?

6. Accessing class member functions from Global Callback

7. Getting pointer to non-static member function from C callback function

8. Callback functions as member functions

9. CALLBACK Functions as Class Members

10. Making DDEML Callback function a member of a class

11. CALLBACK DLGPROC as a class member function

12. Using Non-Static Callback Functions as member Functions VC5.0

 

 
Powered by phpBB® Forum Software