Boost:Bind//Boost:Function -- Need some help with a constructor trying to bind to a function
I am struggling to try to get bind/function working like I want. Some code first though: 1. We have a POD object. namespace POD{ class POD{};} 2. We have another class that wants to do something with a POD*. namespace Alpha{ class MethodsInHere{ void doesSomething(POD::POD* arg0){} };} 3. We have a third class, essentially a function handler, that has a boost::function reference whose signature matches the method in #2, and a constructor which wants to set the reference. namespace EventHandler{ class EventHandler{ public: EventHandler(boost::function<void (POD::POD*)>& function): assign_to_me(function){} boost::function<void (POD::POD*)>& assign_to_me; };} 4. Finally, we have an initialization class. This class wants to instantiate the EventHandler object, and to do so needs to bind the method in #2 to pass in as an argument. POD::POD* instance = new POD::POD(); Alpha::MethodsInHere mih; EventHandler eh(boost::bind(&Alpha::MethodsInHere::doesSomething, ????, _1)) -------- My question is basically if this is possible. I was having a ton of trouble getting this to work, and after digging through some docs it seems like the '?????' in line of code in #4 needs to be a reference to eh . . . which isn't constructed yet. If I'm wrong, please tell me how to construct this bind properly. If I'm right, what sort of workaround could solve this?
Charles Milutinovic <kssarh <at> gmail.com> writes: [snip]
namespace Alpha{ class MethodsInHere{ void doesSomething(POD::POD* arg0){} };}
[snip]
namespace EventHandler{ class EventHandler{ public: EventHandler(boost::function<void (POD::POD*)>& function) : assign_to_me(function){} boost::function<void (POD::POD*)>& assign_to_me; };}
<aside> I don't think you want assign_to_me to be a reference, but just hold by value. Otherwise you're holding a reference to a temporary below. </aside>
Alpha::MethodsInHere mih; EventHandler eh(boost::bind(&Alpha::MethodsInHere::doesSomething, ????, _1))
--------My question is basically if this is possible. I was having a ton of trouble getting this to work, and after digging through some docs it seems like the '?????' in line of code in #4 needs to be a reference to eh . . . which isn't constructed yet. If I'm wrong, please tell me how to construct this bind properly. [snip]
Sorry, but you're wrong. You want "????" to be "mih", not "eh". "doesSomething"'s type is basically "(Alpha::MethodsInHere&, POD::POD*) -> void" hence you need and instance of Alpha::MethodsInHere. HTH, -Ryan
You're absolutely right about the reference. That led to a process of chasing bugs in much more complex code than in my example that made me sprint right past the way to properly use the boost::bind/boost::function pair I needed in my event handler because I attributed some seg faults to it. Do not code while half asleep! Thank you for your and Alan's reply. On Sun, Nov 2, 2008 at 6:57 PM, Ryan Gallagher <ryan.gallagher@gmail.com>wrote:
Charles Milutinovic <kssarh <at> gmail.com> writes: [snip]
namespace Alpha{ class MethodsInHere{ void doesSomething(POD::POD* arg0){} };}
[snip]
namespace EventHandler{ class EventHandler{ public: EventHandler(boost::function<void (POD::POD*)>& function) : assign_to_me(function){} boost::function<void (POD::POD*)>& assign_to_me; };}
<aside> I don't think you want assign_to_me to be a reference, but just hold by value. Otherwise you're holding a reference to a temporary below. </aside>
Alpha::MethodsInHere mih; EventHandler eh(boost::bind(&Alpha::MethodsInHere::doesSomething, ????, _1))
--------My question is basically if this is possible. I was having a ton of trouble getting this to work, and after digging through some docs it seems like the '?????' in line of code in #4 needs to be a reference to eh . . . which isn't constructed yet. If I'm wrong, please tell me how to construct this bind properly. [snip]
Sorry, but you're wrong. You want "????" to be "mih", not "eh". "doesSomething"'s type is basically "(Alpha::MethodsInHere&, POD::POD*) -> void" hence you need and instance of Alpha::MethodsInHere. HTH,
-Ryan
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Charles Milutinovic
-
Ryan Gallagher