boost的库中bind和function使用实例

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
using namespace std;

class BDemo {
public:
	typedef boost::function<void(int)> CallbackFun;
	BDemo() {
		cout << "BDemo()" << endl;
	}
	~BDemo() {
		cout << "~BDemo()" << endl;
	}
	//设置回调函数对象
	void set_CallBack(CallbackFun callfun) {
		callFun_ = callfun;
		cout << "BDemo::setCallBack" << endl;
	}
	//调用回调函数对象
	void do_CallBack(int num) {
		callFun_(num);
	}
private:
	CallbackFun callFun_;
};
class ADemo {
public:
	ADemo(int x) :
			x_(x), bdemo_(new BDemo) {
		cout << "ADemo()" << endl;
		//boost::bind(&Point::setX,this,_1)(100);
		bdemo_->set_CallBack(boost::bind(&ADemo::setX, this, _1));
	}
	~ADemo() {
		cout << "~ADemo()" << endl;
	}
	void setX(int x) {
		x_ = x;
		cout << "setX:" << x << endl;
	}

	void print(int n) {
		bdemo_->do_CallBack(n);
		cout << "x:" << x_ << endl;
	}
private:
	int x_;
	//BDemo* bdemo_;
	//boost::shared_ptr<BDemo> bdemo_;
	boost::scoped_ptr<BDemo> bdemo_;
};

int main() {

	/*   boost::bind(testFunc,_1,_2)(1,3);*/
	ADemo ad(0);
	ad.print(10000);
	//RectDemo rec;
	//rec.funt();
	//boost::bind(&Point::print,p1)(rectDe_);
	//boost::bind(&Point::setX,p1,_1)(20);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值