Callbacks

Hello ladies & gentlemen :)

I'm currently developing a GUI system for the SFML library. Consequently, I will need to provide a way to bind functions to certain events. I thought about using callbacks, but then ran into this problem: How do I store a function pointer to a class member function. I'm using typedef boost::function<void()> callback as my signature and it works for non-member functions.

Is there an elegant solution for this problem?

Xander
Last edited on
I don't think C++ support it. Theoretically, a non-static member function has to be executed with an instance of its host class, in other word, it doesn't make sense to call a non-static member function without an instance of a class.

To resolve this problem, you need to define a static member function in that class as the callback, then somehow let the caller to pass an instance of that class to the callback function.
How consistent are the arguments to the functions? You could use a C'ish solution and cast the functions to void pointers, this should be the address of the function particular to that instance of the object. You would need to be consistant with the parameter list, or use that elipse thing in C that I've never quite gotten to work right. I haven't tested this solution on as large of a scale as what you are suggesting but I'm about to run some tests with the basic idea.
Wow, I could not have expected a better answer to this question from my compiler:
"ISO C++ forbids taking the address of a bound member function to form a pointer to member function..."

I guess that shoots my idea down.


EDIT: I remember! I had an issue sort of like this. I ended up storing an array of pointers to the base class that my other classes inherited from and ran the functions that way.
Last edited on
Topic archived. No new replies allowed.