I am trying to encapsulate the binding and connection of a boost signal (which requires the method to call and the instance the method belongs to) within a class method. So ideally I'd like to say something like,
// button.cpp
void Button::addListener(callback, object) { // <-- how is the callback and object defined here
boost::signal<void()> signal;
signal.connect(boost::bind(callback, object));
}
I understand that you cannot pass a reference to the instance version of app.onClick to btn.addListener. That being said then, I'm not sure it is possible to encapsulate the boost code from outside of App. If it is possible though, how would the signature for Button::addListener() look?
Oh, excellent. I'm glad the general principle is possible, at least.
I'm going to check out the other idea of the boost::function next but I just need some clarification on where to put the template code. Putting the implementation of the method in the header file works great like so:
but what if the implementation was going to be in the button.cpp file? Both the header and implementation files will need to know what the definition of Fn and Obj are.
With the template definition included just above the method in button.cpp the compiler points out that it has already been defined in the header file. Without it, it doesn't know what Fn and Obj are. Presumably there still needs to be a reference to the template in the .cpp file but I can't see where.