C++ Boost signal2信号/插槽

#include "stdafx.h"
#include "boost/signals2.hpp"
#include "boost/bind.hpp"
#include "boost/function.hpp"
#include <iostream>

using namespace std;
class Button{
private:
typedef boost::signals2::signal<void(int,int)> singalDef;
typedef singalDef::slot_type slotType;
private:
//信号
singalDef m_singal;

boost::signals2::connection m_connect;
public:
//信号的绑定函数
boost::signals2::connection Connect(const slotType& slot){
return m_singal.connect(slot);
}
//信号的发射
void SendSignal(int x,int y){
m_singal(x,y);
}
};

//对button的信号进行处理
class ButtonPro{
public:
ButtonPro(Button* button):m_button(button){
//绑定信号和槽
m_button->Connect(boost::bind(&ButtonPro::slotButton,this,_1,_2));
}
//槽函数
void slotButton(int x,int y){
cout<<"输出:"<<x<<", "<<y<<endl;
}
private:
Button* m_button;
};

int _tmain(int argc, _TCHAR* argv[])
{
Button* pb = new Button();
ButtonPro bp(pb);
//发出一个信号
pb->SendSignal(1,2);
delete pb;
return 0;
}

  

posted @ 2018-05-24 19:44  `野百合的春天  阅读(1493)  评论(0编辑  收藏  举报