Skip to main content
GameDev.net gamedev.net
🔒 Locked

Member function as callback function

Started by yanuart Oct 13, 2005 at 11:43 PM 4 replies 1.9k views
Original Post
yanuart
yanuart
I'm trying to make a wrapper class for DInput but apparently I can't assign member function as callback function. How do you guys make callback function inside a class ? a sample of my code :

class CInput
{
	// these are callback function inside a class
	BOOL CALLBACK    EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext );
	BOOL CALLBACK    EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext );

};
and this is where it won't compiled :

if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, 
                                         EnumJoysticksCallback,
                                         NULL, DIEDFL_ATTACHEDONLY ) ) )
        return hr;
Ride the thrill of your life
Play Motorama
circlesoft
circlesoft
Unfortunately, you need to make a regular C function, then just pass it through to your member function...like this:

BOOL CALLBACK EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext ){   return myInputInstance->EnumObjectsCallback( pdidoi, pContext );}


If you were defining your own callbacks, you could use member functions initially. However, since DInput has already defined them for you, you can't.
smitty1276
smitty1276
You can also use static member functions as callbacks. If you keep a static pointer to your instance, that static method can then call the appropriate method of that instance. Just remember that the static method can only refer to other static members (hence the instance pointer).
yanuart
yanuart
I try to make those callback functions static but then i have to make Dinput interfaces static too.
Everythig seems fine but after I try to link it I got all these weird unresolved external link error ??
what happened ?
Ride the thrill of your life
Play Motorama
Muhammad Haggag
Muhammad Haggag
Quote:
Original post by yanuart
I try to make those callback functions static but then i have to make Dinput interfaces static too.
Everythig seems fine but after I try to link it I got all these weird unresolved external link error ??
what happened ?

1. Static member variables are only declared inside the class definition. They have to be defined outside.
class Example{static int m_x;    // declaration};int Example::m_x;    // definition


2. Make the callback function static, but don't make your member variables (DInput interfaces) static. To access the member variables from the static function, pass the address of the object containing the variables through the pContext parameter, then cast it back to your object's type in the callback.
class CInput{    // these are callback function inside a class    static BOOL EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext)    {        CInput* input_object = static_cast<CInput*>(pContext);        ...    }};...CInput input_object;...if(FAILED(hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL,                                          EnumJoysticksCallback,                                         &input_object, DIEDFL_ATTACHEDONLY ) ) )        return hr;

yanuart
yanuart
Coder : thanks man ! Solve the problem.. woot woot !
Ride the thrill of your life
Play Motorama

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.