#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;
}