Timeline for pointers to member functions in an event dispatcher
Current License: CC BY-SA 3.0
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 28, 2013 at 4:22 | review | Suggested edits | |||
| Jun 28, 2013 at 4:24 | |||||
| Oct 4, 2012 at 20:19 | comment | added | Neil M | Thanks QuasarDonkey, you beat me to it. You're right, it's pretty ugly but it's commonly used in the Windows API, pthreads and lower level Mac OS X APIs and... everywhere! I usually hide the free functions that handles the callbacks in my implementation files in the anonymous namespace. C# style delegates calling back to members looks nicer, but it just got in the way of getting things done for me. | |
| Oct 4, 2012 at 18:17 | comment | added | Daniel Hanrahan | @derivative The free function approach is just using function pointers. What Neil is saying is to include a userdata parameter, e.g. have a static member function static void MyClass::Handler( Event* event, void* userdata ). Inside it, cast userdata to MyClass. It will then have access to MyClass's internals. But you'll also need to store the userdata argument along with the callback: dispatcher->setCallback( &MyClass::Handler, (void*)myClassObj );. You can even have the static callback invoke a private member: ((*MyClass)userdata)->RealHandler(event);. It's ugly but it works | |
| Oct 2, 2012 at 23:16 | comment | added | derivative | It looks like a combination of std::function and std::bind can get me the desired functionality... I didn't think such things were in the standard library. Can you elaborate on how you use a free function? | |
| Oct 2, 2012 at 23:04 | history | answered | Neil M | CC BY-SA 3.0 |