自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++零基礎(chǔ)教程之std:function函數(shù)包裝器

開發(fā) 后端
C++提供了std::function和std::bind統(tǒng)一了可調(diào)用對象的各種操作。不同類型可能具有相同的調(diào)用形式。使用前記得加上functional頭文件。

[[411610]]

前言

C++中可調(diào)用對象的雖然都有一個比較統(tǒng)一的操作形式,但是定義方法五花八門,這樣就導(dǎo)致使用統(tǒng)一的方式保存可調(diào)用對象或者傳遞可調(diào)用對象時,會十分繁瑣。C++提供了std::function和std::bind統(tǒng)一了可調(diào)用對象的各種操作。不同類型可能具有相同的調(diào)用形式。使用前記得加上functional頭文件。

包裝普通函數(shù)

  1. #include <iostream> 
  2. #include <string> 
  3. #include <functional> 
  4. using namespace std; 
  5. int Max(int a, int b)  
  6.     return a > b ? a : b; 
  7. void print()  
  8.     cout << "無參無返回值" << endl; 
  9. int main() 
  10.   function<int(intint)> funMax(Max); 
  11.     cout << funMax(1, 2) << endl; 
  12.     function<void()> funPrint(print); 
  13.     print(); 
  14.     printData(funMax, 1, 2); 
  15.   return 0; 

包裝類的靜態(tài)方法

  1. #include <iostream> 
  2. #include <string> 
  3. #include <functional> 
  4. using namespace std; 
  5. class Test  
  6. public
  7.     static void  print(int a, int b)  
  8.     { 
  9.         cout << a + b << endl; 
  10.     } 
  11.     void operator()(string str)  
  12.     { 
  13.         cout << str << endl; 
  14.     } 
  15.     operator FuncPTR()  
  16.     { 
  17.         return print; 
  18.     } 
  19. }; 
  20. int main()  
  21.     //包裝類的靜態(tài)方法 
  22.     function<void(intint)> sFunc = Test::print; 
  23.     sFunc(1, 2); 
  24.     return 0; 

包裝仿函數(shù)

  1. #include <iostream> 
  2. #include <string> 
  3. #include <functional> 
  4. using namespace std; 
  5. class Test  
  6. public
  7.     void operator()(string str)  
  8.     { 
  9.         cout << str << endl; 
  10.     } 
  11. }; 
  12. int main()  
  13.     //包裝仿函數(shù) 
  14.     Test test; 
  15.     function<void(string)> funTest = test; 
  16.     test("仿函數(shù)"); 
  17.     return 0; 

包裝轉(zhuǎn)換成函數(shù)指針的對象 (operator的隱式轉(zhuǎn)換)

  1. #include <iostream> 
  2. #include <string> 
  3. #include <functional> 
  4. using namespace std; 
  5. using FuncPTR = void(*)(intint); 
  6. class Test  
  7. public
  8.   static void  print(int a, int b)  
  9.     { 
  10.         cout << a + b << endl; 
  11.     } 
  12.     operator FuncPTR()  
  13.     { 
  14.         return print; 
  15.     } 
  16. }; 
  17. int main()  
  18.     //包裝轉(zhuǎn)換成函數(shù)指針的對象  (operator的隱式轉(zhuǎn)換) 
  19.     Test object; 
  20.     function<void(int,int)> funOPE = object; 
  21.     funOPE(2, 3); 
  22.     return 0; 

 

責任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2021-02-06 07:49:48

C語言編程開發(fā)技術(shù)

2020-04-09 14:02:33

NginxHttps前端

2021-04-25 08:11:57

C語言常量與變量標識符命名規(guī)范

2020-09-30 14:04:25

C++運算符重載

2021-05-07 09:52:29

C語言運算符表達式

2017-07-18 10:14:23

OracleMerge into教程

2017-07-18 14:40:05

大數(shù)據(jù)數(shù)據(jù)可視化

2016-08-31 14:01:31

MySQL存儲數(shù)據(jù)庫

2022-09-19 08:12:47

編譯器程序函數(shù)

2009-06-22 09:23:18

事件監(jiān)聽器

2021-02-20 06:13:18

C 語言C++

2021-02-21 12:09:32

C 語言基礎(chǔ)語法

2021-04-13 08:42:29

C語言數(shù)據(jù)類型轉(zhuǎn)換自動類型轉(zhuǎn)換

2021-02-08 20:25:12

C 語言C++Linux

2021-02-11 08:25:17

C 語言C++ 基礎(chǔ)

2021-02-16 10:57:34

C++ C 語言windows

2009-07-24 09:20:15

數(shù)組實例

2011-04-15 09:20:56

ASP.NET MVC

2020-06-10 10:50:48

C++開發(fā)編程

2009-07-24 10:09:08

ASP.NET個性化ASP.NET基礎(chǔ)教程
點贊
收藏

51CTO技術(shù)棧公眾號