Elegent way to use member function as callback?

Hi all,
I'm using a library that takes a callback function of type (void (int)) for notifying the library user about an event. Is there a good way to use the member function of a class as this callback?

If I declare a member function (void (int)) it isn't the same type as a C-style function declared in the same way, and I can't cast it. I suspect this is because the member function needs to be altered to take the appropriate object as one of it's parameters.

Thinking along these lines, I should be able to cast a static member function to the appropriate type, but unfortunately this doesn't seem to be working either. The cast of
static void m_fMemberCallback(int iParam);
to
(void (int)) m_fMemberCallback
results in an "invalid cast to function type 'void()(int)'" error from the compiler.

I know I can get around this by just declaring a C-style function, then calling my static member function, but I'm curious if there is a better way to do this.

Cheers,

Jivan
You'll learn all about this kind of stuff, and cool ways to do it, over at
The Function Pointer Tutorials
http://www.newty.de/fpt/index.html

Hope this helps.
Hi Duoas,
Thanks for the tip, that looks like a great tutorial. Secion "3.4 How to Implement a Callback to a static C++ Member Function ?" plus the syntax examples helped me solve the problem.
Topic archived. No new replies allowed.