Boost.Signals2:在C++中实现事件处理

11 篇文章 0 订阅
#include <iostream>
#include <boost/signals2/signal.hpp>

using std::cout;
using std::endl;

struct A
{
	void f() {
		int a = 2;
		event(1, &a);
	}
	boost::signals2::signal<void (int, int*)> event;
};

void h5(int i, int* p) {cout << "h5\t" << i << " " << *p << endl;}

struct B
{
	A a;
	void g() {
		a.event.connect(std::bind(&B::h, this, std::placeholders::_1, std::placeholders::_2));
		a.event.connect(std::bind(&B::h, this, std::placeholders::_1, std::placeholders::_2));
		a.event.connect(std::bind(&B::h1, this, std::placeholders::_2, std::placeholders::_1));
		a.event.connect(std::bind(&B::h2, this, 2, std::placeholders::_1, std::placeholders::_2));
		a.event.connect(std::bind(&B::h3, this, std::placeholders::_2));
		a.event.connect(std::bind(&B::h4, this));
		a.event.connect(h5);
		a.event.connect([](int i, int* p) {cout << "h6\t" << i << " " << *p << endl;});
		a.f();
	}
	void h(int i, int* p) {cout << "h\t" << i << " " << *p << endl;}
	void h1(int* p, int i) {cout << "h1\t" << i << " " << *p << endl;}
	void h2(int i, int j, int* p) {cout << "h2\t" << i << " " << j << " " << *p << endl;}
	void h3(int* p) {cout << "h3\t" << *p << endl;}
	void h4() {cout << "h4" << endl;}
};


int main()
{
	B b;
	b.g();

	return 0;
}

/*
h       1 2
h       1 2
h1      1 2
h2      2 1 2
h3      2
h4
h5      1 2
h6      1 2
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值