boost::function的用法


本片文章主要介紹boost::function的用法。 boost::function 就是一個函數的包裝器(function wrapper),用來定義函數對象。

1.  介紹

    Boost.Function 庫包含了一個類族的函數對象的包裝。它的概念很像廣義上的回調函數。其有着和函數指針相同的特性但是又包含了一個調用的接口。一個函數指針能夠在能以地方被調用或者作為一個回調函數。boost.function能夠代替函數指針並提供更大的靈活性。

2. 使用

    Boost.Function 有兩種形式:首選形式和便攜式形式, 其語法如下:

首選形式 便攜式形式
boost::function<float(int x, int y)>f boost::function2<float, int, int>f

但是便攜式形式不是所有的編譯器都支持的, 所以這里我只介紹首選形式。

2.1 普通函數

    我們可以看下如下的例子:

 1  void do_sum(int *values, int n) 
 2  2 { 
 3  3     int sum(0);
 4  4     for (int i = 0; i < n; ++i) 
 5  5     { 
 6  6         sum += values[i]; 
 7  7     } 
 8  8     cout << sum << endl; 
 9  9 };
10 10 int _tmain(int argc, _TCHAR* argv[]) 
11 11 { 
12 12     boost::function<void(int *values, int n)> sum; 
13 13     sum = &do_sum;
14 14     int a[] = {1,2,3,4,5}; 
15 15     sum(a, 5);
16 16     return 0; 
17 17 }

 

 

  sum 可以理解為一個廣義的函數對象了,其只用就是保存函數do_sum, 然后再調用之。

2.2 成員函數

    在很多系統中, 對於類的成員函數的回調需要做特殊處理的。這個特殊的處理就是“參數綁定”。當然這個超出了我們討論的范圍了。 boost::function對於成員函數的使用可以看下如下代碼:

 1 class X{ 
 2  2 public: 
 3  3     int foo(int a) 
 4  4     { 
 5  5         cout << a <<endl; 
 6  6         return a; 
 7  7     } 
 8  8 };
 9  9 int _tmain(int argc, _TCHAR* argv[]) 
10 10 { 
11 11     boost::function<int(X*, int)>f;
12 12     f = &X::foo;
13 13     X x; 
14 14     f(&x, 5);
15 15     return 0; 
16 16 }

 

 

    我們發現, 對類的成員函數的對象化從語法是沒有多大的區別。

3. 一個典型的例子

    上面的幾個例子沒有體現出boost::function的作用來, 這里在寫一個例子。比如當程序執行到某一處的時候想綁定某一個函數, 但是不想立即執行, 我們就可以聲明一個函數對象,給此對象綁定相應的函數, 做一些其他事情,然后再來執行綁定的函數, 代碼如下:

 1 void print(int a) 
 2  2 { 
 3  3     cout << a << endl; 
 4  4 }
 5  5 typedef boost::function<void (int)> SuccessPrint;
 6  6 int _tmain(int argc, _TCHAR* argv[]) 
 7  7 { 
 8  8     vector<SuccessPrint> printList;
 9  9     SuccessPrint printOne = boost::bind(print, _1); 
10 10      printList.push_back(printOne); 
11 11     SuccessPrint printTwo = boost::bind(print, _1); 
12 12     printList.push_back(printTwo); 
13 13     SuccessPrint printThree = boost::bind(print, _1); 
14 14     printList.push_back(printTwo); 
15 15     // do something else
16 16     for (int i = 0; i < printList.size(); ++i) 
17 17         printList.at(i)(i);
18 18     return 0; 
19 19 }

 

     上述代碼中首先把聲明一個函數對象 typedef boost::function<void (int)> SuccessPrint, 然后把print綁定到斥對象中, 放入vector中, 到最后才來執行這print()函數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM