boost::bind 用法

原文地址:http://www.cnblogs.com/haichang/archive/2010/10/22/1857974.html

01 #include <iostream>
02 #include <boost/bind.hpp>
03 #include <boost/function.hpp>
04  
05 class Test
06 {
07   public:
08     void test()
09     {
10       std::cout<<"test"<<std::endl;   
11     }
12     void test1(int i)
13     {
14       std::cout<<"test1:"<<i<<std::endl;
15     }
16     void test2(int a,int b)
17     {
18       std::cout<<"test2:a:"<<a<<" b:"<<b<<std::endl;
19     }
20 };
21 int main()
22 {
23   Test t;
24   boost::function<void()> f1; // 无参数,无返回值
25   f1 = boost::bind(&Test::test, &t);
26   f1(); //调用t.test();
27    
28   f1 = boost::bind(&Test::test1, &t, 2);
29   f1(); // 调用 t.test1(2);
30      
31   boost::function<void(int)> f2;
32   f2 = boost::bind(&Test::test1,&t,_1);
33   f2(3);  //调用t.test1(3)
34    
35   boost::function<void(Test*)> f3;
36   f3 = boost::bind(&Test::test1, _1,4);
37   f3(&t); //调用t.test1(4)
38    
39   boost::bind(&Test::test2, _1,5,6)(&t);    //test2:a:5 b:6
40   boost::bind(&Test::test2, &t,_1,8)(7);    //test2:a:7 b:8
41   boost::bind(&Test::test2, &t,_1,_2)(9,10);  //test2:a:9 b:10
42   boost::bind(&Test::test2, &t,11,_1)(12);    //test2:a:11 b:12
43   return 0;
44 }

原文地址:https://www.cnblogs.com/wangkangluo1/p/2129624.html