Moving CALLBACK function to a class

Started by
1 comment, last by Qw3r7yU10p! 19 years, 4 months ago
I've got a function that initializes an array of strings with the names of all of the keys available on the detected keyboard. This is fine, and it works:

BOOL CALLBACK DIEnumDeviceObjectsProc(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef)
{
	keyboard->keyNames2[lpddoi->dwOfs] = lpddoi->tszName;

	return DIENUM_CONTINUE;

} // end DIEnumDeviceObjectsProc()

// function called like this:
	IDirectInputDevice2_EnumObjects(keyboard->GetKeyboard(), DIEnumDeviceObjectsProc, &bError, DIDFT_PSHBUTTON);

However, I'm writing a 2D game engine, and would like to move as much code as possible into the actual engine. My engine consists of an Engine class, which handles initialization, shutdown, etc. Everything else is defined in classes, such as Sprite, Sound, Texture, etc. Is there a way to move this CALLBACK function into a class...right now it's defined as a global function in my test project's main() function. I tried moving it to a class but I kept getting a bunch of different errors. I tried making it a static method in the Engine class, and several other things with no success. Thanks.
Life's Short. Make Games.
Advertisement
The short answer is no. The long answer is that it is complicated.

Take a look at this link

Quote:The problem: Why it is not possible to use a member function as callback
There is an important difference between a member function and an ordinary C function or class function (which is much the same as a C function): They use different calling conventions. Where C functions use the _cdecl or _stdcall calling convention, member functions implicitly use the thiscall calling convention. A member function always gets an additional hidden parameter, the this pointer, which contains the address of the object the member function is called for. Because of the necessary hidden this parameter the syntax to call a function through a function pointer differs for C functions and member functions


As you can see, its is very complicated, and I don't know if you want to ump through all the hoops to get it done. Here is some more info on callbacks as well. Here is also another example of trying to do class callbacks.

I don't want to discourage you, but from just browsing those articles they frighten me [lol]. Best of luck!
class MyClass {public:    static BOOL CALLBACK DIEnumDeviceObjectsProc(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) {        //etc    }};


Should work fine.

If you want to call non-static functions on an object of type MyClass you need to get the object from somewhere.

If you can define your own data (which is usually what the LPVOID is used for), then you can use that to pass in the pointer to an actual object, cast it to MyClass and call whatever function you want

class MyClass {public:    void SomeOtherFunction() {    }    static BOOL CALLBACK DIEnumDeviceObjectsProc(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) {        MyClass* obj = static_cast<MyClass*>(pvRef);        obj->SomeOtherFunction();    }};

This topic is closed to new replies.

Advertisement