Define a callback member function inside the object instance

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I would ask you a suggestion. In according with the FAQ (http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2) is better to not set a non-static member function like a callback. But If I need to pass that callback inside my object, I'm pretty sure that the object itself exist! So if I have a callback signature like this: int my_callback(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, int status, void *userData ) And I want to bind it to a member function: int MyObject::my_callback(....) Now, inside the object I have something like: MyObject::OneMethod() { p_AnotherObject->SetCallBack( ???? ); } How can I pass my MyObject::my_callback to SetCallBack using boost::function and/or boost::bind? Thanks! Daniele. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) iD8DBQFKl65//l+kMioSZwgRAkA8AKCdk44mlTXESXQJUG7vzqVnUHFoXgCghGW5 Icf+E8uX8cLwwfkdnCFmoKs= =s1nk -----END PGP SIGNATURE-----

On Fri, Aug 28, 2009 at 4:16 AM, Daniele
Barzotti
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Hi,
I would ask you a suggestion. In according with the FAQ (http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2) is better to not set a non-static member function like a callback.
But If I need to pass that callback inside my object, I'm pretty sure that the object itself exist!
So if I have a callback signature like this:
int my_callback(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, int status, void *userData )
And I want to bind it to a member function:
int MyObject::my_callback(....)
Now, inside the object I have something like:
MyObject::OneMethod() { p_AnotherObject->SetCallBack( ???? ); }
How can I pass my MyObject::my_callback to SetCallBack using boost::function and/or boost::bind?
If the callback is a real function pointer and not boost::function, you cannot. Bind creates a functor, which cannot be placed in a C style function pointer, you need a C++ style function pointer (std/boost::function). Your best bet would be to stick the instance in the userData and call back to your instance.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 OvermindDL1 wrote:
If the callback is a real function pointer and not boost::function, you cannot. Bind creates a functor, which cannot be placed in a C style function pointer, you need a C++ style function pointer (std/boost::function).
Oh. You're right! I didn't thought!
Your best bet would be to stick the instance in the userData and call back to your instance.
Yes, actually I do exactly this! Thanks! Daniele. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) iD8DBQFKl7NE/l+kMioSZwgRAhbkAKDI3OZg+unAeb06464oCHpdsGF0FQCgs6yL XcRK9gQv42nFbK5j+UMJpfg= =EXb2 -----END PGP SIGNATURE-----
participants (2)
-
Daniele Barzotti
-
OvermindDL1