boost的库中bind和function使用实例

#include 
#include 
#include 
#include 
#include 
using namespace std;

class BDemo {
public:
	typedef boost::function 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_;
	boost::scoped_ptr 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;
}

你可能感兴趣的:(Boost库)