|
Boost Users : |
Subject: Re: [Boost-users] a quick question regarding boost::bind, with a test case
From: PB (newbarker_at_[hidden])
Date: 2011-04-12 16:01:57
On Mon, Apr 11, 2011 at 6:44 PM, Littlefield, Tyler <tyler_at_[hidden]> wrote:
> Hello all:
> 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.
>
Your test case seems to be missing the thing that is causing you
problems, and the bits provided don't compile. The following program
works for me:
#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);
}
int main()
{
Math m;
m.Add(5,6,7,boost::bind(&Printer,_1));
}
Regards,
Pete
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