bind member function of a member of a class...

I cannot figure out how to use boost::bind in following scenario: class A { int i_; public: int test() {return i;} }; class B { A _a; public: A& getA() {return _a;} } bool testbind() { B b; // here I am trying to find out if b.getA().test() equals 1 // I understand that B::getA::test is incorrect, and that's my question- // how to use bind here? return (boost::bind (&B::getA::test, b) ==1); }

I'm still a novice with bind but I think you have to bind twice:
#include <iostream>
#include <functional>
#include <algorithm>
#include
I cannot figure out how to use boost::bind in following scenario:
class A { int i_; public: int test() {return i;} };
class B { A _a; public: A& getA() {return _a;} }
bool testbind() { B b;
// here I am trying to find out if b.getA().test() equals 1 // I understand that B::getA::test is incorrect, and that's my question- // how to use bind here? return (boost::bind (&B::getA::test, b) ==1); }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Kevin

That's not a good usage scenario for bind - you should just call the method directly. Bind is useful when you want to either (1) pass the functor to something else or (2) store the functor for future use. Conceptually, as a rough guide, you should think of the result of bind as a pointer, not a value. So, naturally, the result of bind of anything isn't going to be equal to 1, just like you won't have a pointer equal to one. Instead you want to compare the result of invoking / dereferencing the functor, which is done with the function operator (). So your example would work if you added that, e.g. return (boost::bind (&B::getA::test, b)() ==1); But a more standard use case would look like (untested) template < typename F > bool check_property(F const& func, B const& b) { return func(b); } bool testbind() { B b; return check_property(boost::bind(&A::test, boost::bind(&B::getA, _1)) == 1, b); } The == operator of a bind result and a int creates a functor that compares the result of invoking the functor to that int. This passes that to check_property which invokes it with the function operator. P.S. If you want to store a Bind result for later use, look at Boost.Function. These two libraries are duals of each other - Bind to *pass* function objects to somebody else, Function to *receive* function objects from somebody else. At 04:33 PM 3/4/2009, you wrote:
I cannot figure out how to use boost::bind in following scenario:
class A { int i_; public: int test() {return i;} };
class B { A _a; public: A& getA() {return _a;} }
bool testbind() { B b;
// here I am trying to find out if b.getA().test() equals 1 // I understand that B::getA::test is incorrect, and that's my question- // how to use bind here? return (boost::bind (&B::getA::test, b) ==1); }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Alan M. Carroll
That's not a good usage scenario for bind - you should just call the method
want to either (1) pass the functor to something else or (2) store the functor for future use. Conceptually, as a rough guide, you should think of the result of bind as a
naturally, the result of bind of anything isn't going to be equal to 1, just
equal to one. Instead you want to compare the result of invoking / dereferencing the functor, which is done with the function operator (). So your example would work if you added that, e.g.
return (boost::bind (&B::getA::test, b)() ==1);
But a more standard use case would look like (untested)
template < typename F > bool check_property(F const& func, B const& b) { return func(b); }
bool testbind() { B b; return check_property(boost::bind(&A::test, boost::bind(&B::getA, _1)) == 1, b); }
The == operator of a bind result and a int creates a functor that compares
directly. Bind is useful when you pointer, not a value. So, like you won't have a pointer the result of invoking the functor
to that int. This passes that to check_property which invokes it with the function operator.
P.S. If you want to store a Bind result for later use, look at Boost.Function. These two libraries are duals of each other - Bind to *pass* function objects to somebody else, Function to *receive* function objects from somebody else.
At 04:33 PM 3/4/2009, you wrote:
I cannot figure out how to use boost::bind in following scenario:
class A { int i_; public: int test() {return i;} };
class B { A _a; public: A& getA() {return _a;} }
bool testbind() { B b;
// here I am trying to find out if b.getA().test() equals 1 // I understand that B::getA::test is incorrect, and that's my question- // how to use bind here? return (boost::bind (&B::getA::test, b) ==1); }
_______________________________________________ Boost-users mailing list Boost-users <at> lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thank you for the quick reply and comments that I agree with. In the meantime I
managed to make it work with double bind. When I looked at your suggestion I
tried to implement it. Unfortunately I am getting compiled errors. Here is the
complete test example (vc2008):
#include "stdafx.h"
#include <sstream>
#include <algorithm>
#include <vector>
#include

Clearly I responded a bit too quickly. You can't use B::getA::test because there is no such function. There is only B::getA and A::value. Without bind, it would not be b.getA::value(), but b.getA().value(). Note this is *two* method invocations. Therefore you must have *two* calls to Bind to create a functor that performs *two* method invocations. bind(&A::value, bind(&B::getA, _1)) == 1 Sorry for the confusion, I read what I meant, not what you wrote. At 05:52 PM 3/4/2009, you wrote:
boost::bind (&B::getA::value, _1)() == 1);
participants (3)
-
Alan M. Carroll
-
Archie14
-
Kevin Ludwig