Boost logo

Boost Users :

Subject: Re: [Boost-users] a quick question regarding boost::bind with a test case
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-04-11 14:12:15


On 04/11/2011 10:44 AM, Littlefield, Tyler wrote:
> I have a quick question regarding boost::bind, as the subject line says.
> This isn't a very viable example, but it works well enough to show what
> I want.
> I have a callback I am using boost::bind to bind to, and I am passing a
> function object in as shown by the following code:
> #include <cstdio>
> #include <boost/bind.hpp>
> #include <boost/function.hpp>
> class Math
> {
> public:
> void Add(int a, int b, int c, boost::function<void (int)> printer);
> {
> printer(a+b+c);
> }
> };
> void Printer(int p)
> {
> printf("%d\n", p);
> }How might I make this work? I am not sure how I can pass that as the
> fourth argument so that the function can be called like (1,2,3) etc.
> Also, in the actual problem I moved the function object to the first
> argument, (which just ment changing the prototype and actual function),
> but I still got errors.

I'm not sure exactly what you're trying to do,
but the following should work:
boost::bind(&Math::Add, Math(), _1, _2, _3, &Printer)(1, 2, 3)

In Christ,
Steven Watanabe

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at