将具有绑定成员函数的std :: bind对象传递给函数

时间:2016-04-14 23:32:20

标签: c++ c++11 boost std-function stdbind

我想通过公共函数将回调函数连接到boost信号。我可以传递一个函数指针,但如果我尝试使用std :: bind传递一个成员函数,它将无法编译。给我错误说没有可行的转换。我应该使用什么类型的App :: SetCallback函数参数?

#include <functional>
#include <boost/signal.hpp>
using namespace std::placeholders;  // for _1, _2, _3...

//plain simple call back function
void SimpleCallback(int value) {
//do nothing
}
//class contains a boost::signal, set callback through a public function
class App {
public:
    App() : sig_()
    {}
    typedef boost::signal<void (int value)> SigType;
    typedef std::function<void (int value)> CallbackFunType;
    //connect signal to a callback function
    void SetCallback(CallbackFunType callback) {
        sig_.connect(callback);
    }
//private: //comment this out for testing purpose.
    SigType sig_; //this is the boost::signal
};

//class that has member callback function
class MyCallback {
public:
    MyCallback():
    val(0), app()
    {}
    void MemberCb(int value){
      val = value;
    }
    void Connect() {
        auto bind_fun = std::bind(&MyCallback::MemberCb, this, _1);
        app.SetCallback(bind_fun); //this will not compile, no viable conversion
        app.sig_.connect(bind_fun); //this is fine
        app.SetCallback(SimpleCallback); //this is fine
    }
private:
    int val;
    App app;
};

int main(int argc, char **argv) {
    MyCallback my_cb;
    my_cb.Connect();
    return 1;
}

---------------- UPDATE -----------------

更仔细地阅读增强信号文档,我了解到我可以通过插槽类型。这解决了我的问题

#include <functional>
#include <boost/signal.hpp>
using namespace std::placeholders;  // for _1, _2, _3...

//plain simple call back function
void SimpleCallback(int value) {
//do nothing
}
//class contains a boost::signal, set callback through a public function
class App {
public:
    App() : sig_()
    {}
    typedef boost::signal<void (int value)> SigType;
    typedef SigType::slot_type CallbackFunType;
    //typedef std::function<void (int value)> CallbackFunType;
    //connect signal to a callback function
    void SetCallback(CallbackFunType callback) {
        sig_.connect(callback);
    }
//private: //comment this out for testing purpose.
    SigType sig_; //this is the boost::signal
};

//class that has member callback function
class MyCallback {
public:
    MyCallback():
    val(0), app()
    {}
    void MemberCb(int value){
      val = value;
    }
    void Connect() {
        auto bind_fun = std::bind(&MyCallback::MemberCb, this, _1);
        app.SetCallback(bind_fun); //using SigType::slot_type
        app.sig_.connect(bind_fun);
        app.SetCallback(SimpleCallback);
    }
private:
    int val;
    App app;
};

int main(int argc, char **argv) {
    MyCallback my_cb;
    my_cb.Connect();
    return 1;
}

1 个答案:

答案 0 :(得分:0)

更仔细地阅读增强信号文档,我了解到我可以通过插槽类型。这解决了我的问题

#include <functional>
#include <boost/signal.hpp>
using namespace std::placeholders;  // for _1, _2, _3...

//plain simple call back function
void SimpleCallback(int value) {
//do nothing
}
//class contains a boost::signal, set callback through a public function
class App {
public:
    App() : sig_()
    {}
    typedef boost::signal<void (int value)> SigType;
    typedef SigType::slot_type CallbackFunType;
    //typedef std::function<void (int value)> CallbackFunType;
    //connect signal to a callback function
    void SetCallback(CallbackFunType callback) {
        sig_.connect(callback);
    }
//private: //comment this out for testing purpose.
    SigType sig_; //this is the boost::signal
};

//class that has member callback function
class MyCallback {
public:
    MyCallback():
    val(0), app()
    {}
    void MemberCb(int value){
      val = value;
    }
    void Connect() {
        auto bind_fun = std::bind(&MyCallback::MemberCb, this, _1);
        app.SetCallback(bind_fun); //using SigType::slot_type
        app.sig_.connect(bind_fun);
        app.SetCallback(SimpleCallback);
    }
private:
    int val;
    App app;
};

int main(int argc, char **argv) {
    MyCallback my_cb;
    my_cb.Connect();
    return 1;
}