#include<boost/function.hpp>
class demo_class{
private:
typedef std::tr1::function<void(int)> func_t;
func_t func;
int n;
public:
demo_class(int _n):n(_n){}
template<typename CallBack>
void accept(CallBack f){
func=f;
}
void run(){
func(n);
}
};
void callBackFunc(int i){
cout<<i<<endl;
}
int main(){
using namespace boost::assign;
demo_class dc(10);
dc.accept(callBackFunc);
dc.run();
std::system("pause");
return 0;
}
调用函数对象:
class call_back_obj{
private:
int x;
public:
call_back_obj(int _x):x(_x){}
void operator()(int i){
cout<<i*x++<<endl;
}
};
int main(){
using namespace boost::assign;
demo_class dc(10);
call_back_obj cbo(2);
dc.accept(cbo);
dc.run();
std::system("pause");
return 0;
}