0

Is there a way to avoid the use of boost::bind to attach a member function to a boost::signal slot?

The only way I can get it to work is to use bind like this:

mysignal.connect(boost::bind(&myClass::listenerMember, this, _1, _2));

but I really want it to look more like this:

mysignal.connect(myClass::listenerMember);

Here is some sample code to show it a bit better:

#include <iostream>
#include <cstdlib>
#include "boost/signals2.hpp"

class window
{
    public:
    boost::signals2::signal<void(int,int)>  sigLButtonDown;

    void simulateCallback(){ sigLButtonDown(1,3);}
};

class windowListener
{
    public:
    windowListener(window * pwindow) { pwindow->sigLButtonDown.connect(boost::bind(&windowListener::listenerMember, this, _1, _2));}


    void listenerMember(int x, int y) {    std::cout << "ping!" << std::endl;}

};

int main()
{
    window w;
    windowListener l(&w);

    std::cout << "Here goes!" << std::endl;    
    w.simulateCallback();

}
Jimbo
  • 85
  • 7
  • 1
    How would the slot know which object becomes `*this` when invoking `listenerMember`? – JaMiT Mar 28 '20 at 04:08
  • That was a helpful rhetorical question, thank you. I first wanted to reply that we'd just send in this->listenerMember but then after playing with it and some googling I learned that a function pointer is not the same thing as a member function pointer. So that is just never going to work. I see why I need to use boost::bind now. – Jimbo Mar 28 '20 at 16:41

0 Answers0