Re: [Plib-users] Callback
Brought to you by:
sjbaker
From: Steve B. <sjb...@ai...> - 2001-07-22 21:15:05
|
Arne Kreutzmann wrote: > > Hi, > > I'm lerning plib right now, and have one question. > > button->setCallback ( widget_cb ); > // "void widget_cb ( puObject *ob )" > > I would like classes to handel the callback. > I know I can do that if I use *static*, is there an another way ? No - there is a real problem with the C++ language - this is not something you can solve. The bottom line is that C++ does not allow you to take the address of a non-static member function...and there are good reasons for this. You end up doing this: * Write a short 'wrapper' function: void my_callback ( puObject *ob ) { MyClass *my_class = (MyClass *)(ob->getUserData()) ; my_class -> memberFunction ( ob ) ; } (This could be a static member function of the class) * Put a pointer to the class to which the button belongs into the userdata field of the button. button -> setUserData ( this ) ; * Make the wrapper function be the callback: button -> setCallback ( my_callback ) ; This is a pain - but it's the best you can do in C++. ----------------------------- Steve Baker ------------------------------- HomeMail : <sjb...@ai...> WorkMail: <sj...@li...> HomePage : http://web2.airmail.net/sjbaker1 Projects : http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net http://agtoys.sf.net http://prettypoly.sf.net http://freeglut.sf.net http://toobular.sf.net |