Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

WildWest

DInput callback problem

This topic is 5542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing an input class for DirectInput. I know that when you put the callback fucntion within this class it has to be static. But when I do this and call another member function out of my class which is not static I get this complie error: error C2352: Illegal call of non-static member function. How can I solve this problem without making this function static too or using a global function? Any ideas? I hope you all can help me, cause I want to finish this class tonight... ;-) The Wild Wild West - Desperado!

Share this post


Link to post
Share on other sites
Advertisement
Use the void "context" pointer to the callback to pass the "this" pointer for the particular instance of the class.


void CBlah::start() // <-- a non-static member
{
pDI->Blah( blah,..., CBlah::theCallback, (void *)this );
}


static HRESULT CBlah::theCallback( blah,..., LPVOID context )
{
CBlah *pthis = (CBlah *)context;

// access a member variable
pthis->m_aMemberVariable = 12345;

// call a non-static member function
return pthis->aNonStaticMemberFunction( blah )
}


Personally I tend to keep the code in the static callback really lean and do all the actual handling work in a non-static "OnCallback" function so that I don''t need to prepend "pthis->" to everything.

Share this post


Link to post
Share on other sites
Hi!

You can use the method described above by S1CA. When registering the callback, DirectInput allows you to pass a pointer everytime the callback function is called. It is convenient to pass a pointer to the class that way the static callback function can call non static functions.

Another method is to have a global pointer to your class and initialize it in your constructor (using S1CA''s class names):

// Global pointer defined at the top of the CBlah.h file
class CBlah; // Prototype
extern CBlah* g_pCBlah; // extern global pointer

// Then in the CBlah.cpp file
CBlah* g_pCBlah = NULL;

// Finally in the constructor and destructor
CBlah::CBlah() : g_pCBlah(this) { }

CBlah::~CBlah()
{
g_pCBlah = NULL;
}

Now in the static function you can do the following:

static HRESULF CBlah::theCallback(...)
{
assert (g_pCBlah);
g_pCBlah->CallNonStaticClassFunction();
return S_OK;
}

S1CA''s method is cleaner...

Good luck!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!