|
Boost Users : |
From: Kurt Kohler (kohler_at_[hidden])
Date: 2008-09-03 02:39:26
Steven Watanabe wrote:
> AMDG
>
> Kurt Kohler wrote:
>> I have some code that uses pointers to member functions (not
>> boost.function). It compiles and works fine. But when I tried to convert
>> the code to use boost.function, I got a mess of errors.
>> I know I'm doing something wrong, but I have no idea what it is. A clue
>> would be greatly appreciated.
>>
>
> When you use Boost.Function with member functions,
> you need to pass "this" as the first argument.
>
> #include <boost/function.hpp>
>
> struct X
> {
> double f(double x) { return x; }
> double g(double x) { return -x; }
>
> typedef boost::function<double(X*, double)> ftype;
> double call(ftype func, double x)
> {
> return func(this, x);
> }
> };
>
> int main(int argc, char *argv[])
> {
> X x;
> x.call(&X::f, 1.0);
> x.call(&X::g, 2.0);
> }
The test case now compiles! I made the corresponding change to the real
code and that compiles as well.
Thanks a lot!
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