Computer Science Canada Passing member functions as callback arguments |
Author: | Stove [ Sat Nov 08, 2008 3:49 pm ] | ||
Post subject: | Passing member functions as callback arguments | ||
Hello, I have a quick question regarding how to pass member functions as callback arguments. I'm currently building a small class in Visual C++ 2008 to handle the keyboard state using glutKeyboardFunc() and glutKeyboardUpFunc(), however when I attempt to use a member function as one of the arguments for either of the two glut functions I get the error: " 'KeyboardState::KeyDown': function call missing argument list; use '&KeyboardState::KeyDown' to create a pointer to member " (<a href="http://msdn.microsoft.com/en-us/library/b0x1aatf(VS.80).aspx">C3867</a>).
I followed the compiler error's suggestion and passed the address of the function, but that changes it's type to "(__thiscall KeyboardState::*)(unsigned int, int, int)" instead of "(__cdecl *)(unsigned int, int, int)" which doesn't work. Changing the function types to static works, but I'm not sure how to access the non-static variables from them. So, is there somehow a way to convert '&KeyboardState::KeyUp' back to (__cdecl*)? Or is there a way to access non static variables from a static function? Or should I do something entirely different? As you can probably tell I'm not very experienced with C++ and I really have no idea how to solve this, so any help would be greatly appreciated. Thanks. -Stove |
Author: | pavol [ Sat Nov 08, 2008 6:26 pm ] |
Post subject: | RE:Passing member functions as callback arguments |
I'm not sure how different Visual C++ is from other C++ so I don't know if this is a concern. But, my first question, what is glutKeyboardFunc()...is it a predefined function elsewhere? Also why are you trying to pass a function as an argument to another function? |
Author: | md [ Sat Nov 08, 2008 6:27 pm ] |
Post subject: | RE:Passing member functions as callback arguments |
Member functions have an implicit argument called this which points to the classes data members. In order to use a member function you need to either use a non-member friend function, or a static member function (which does not get passed a this pointer). |
Author: | OneOffDriveByPoster [ Sat Nov 08, 2008 7:54 pm ] |
Post subject: | Re: Passing member functions as callback arguments |
I would guess that there should only be one KeyboardState in your program. The Singleton design pattern would help ensure that there is only one KeyboardState object. You can then arrange for the callback to be a static member function that accesses the members of your class by referencing the one KeyboardState object. |
Author: | Stove [ Sat Nov 08, 2008 9:54 pm ] |
Post subject: | Re: Passing member functions as callback arguments |
@ pavol Sorry, glutKeyboardFunc() subscribes a function to be called every time an ascii key is pressed. Its part of the OpenGL utility tool kit. @ md Apparently the 'this' pointer is why C++ functions are incompatible with C callbacks, which is probably why this won't work properly even with the compiler's suggestion. @ OneOffDriveByPoster That sounds perfect for what I'm doing, I'll have to give it a try. |
Author: | md [ Sat Nov 08, 2008 10:02 pm ] |
Post subject: | Re: Passing member functions as callback arguments |
Stove @ 2008-11-08, 9:54 pm wrote: @ pavol
Sorry, glutKeyboardFunc() subscribes a function to be called every time an ascii key is pressed. Its part of the OpenGL utility tool kit. @ md Apparently the 'this' pointer is why C++ functions are incompatible with C callbacks, which is probably why this won't work properly even with the compiler's suggestion. @ OneOffDriveByPoster That sounds perfect for what I'm doing, I'll have to give it a try. The this pointer only exists for non-static member functions - a non-member function or a static member function to not get passed a implicit this pointer and would work perfectly. |
Author: | Stove [ Sun Nov 09, 2008 4:33 pm ] |
Post subject: | Re: Passing member functions as callback arguments |
Opps, yeah, I meant non-static member functions are incompatible. Anyways, I got it working with a global static class like OneOffDriveByPoster suggested, thanks for your help everyone! |