Boost logo

Boost Users :

Subject: Re: [Boost-users] Define a callback member function inside the object instance
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-08-28 06:24:03


On Fri, Aug 28, 2009 at 4:16 AM, Daniele
Barzotti<daniele.barzotti_at_[hidden]> wrote:
> -----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.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net