C++零基础教程之std:function函数包装器

开发 后端
C++提供了std::function和std::bind统一了可调用对象的各种操作。不同类型可能具有相同的调用形式。使用前记得加上functional头文件。

[[411610]]

前言

C++中可调用对象的虽然都有一个比较统一的操作形式,但是定义方法五花八门,这样就导致使用统一的方式保存可调用对象或者传递可调用对象时,会十分繁琐。C++提供了std::function和std::bind统一了可调用对象的各种操作。不同类型可能具有相同的调用形式。使用前记得加上functional头文件。

包装普通函数

  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; 

包装类的静态方法

  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.     //包装类的静态方法 
  22.     function<void(intint)> sFunc = Test::print; 
  23.     sFunc(1, 2); 
  24.     return 0; 

包装仿函数

  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.     //包装仿函数 
  14.     Test test; 
  15.     function<void(string)> funTest = test; 
  16.     test("仿函数"); 
  17.     return 0; 

包装转换成函数指针的对象 (operator的隐式转换)

  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.     //包装转换成函数指针的对象  (operator的隐式转换) 
  19.     Test object; 
  20.     function<void(int,int)> funOPE = object; 
  21.     funOPE(2, 3); 
  22.     return 0; 

 

责任编辑:姜华 来源: 今日头条
相关推荐

2021-02-06 07:49:48

C语言编程开发技术

2020-04-09 14:02:33

NginxHttps前端

2021-04-25 08:11:57

C语言常量与变量标识符命名规范

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

大数据数据可视化

2022-09-19 08:12:47

编译器程序函数

2016-08-31 14:01:31

MySQL存储数据库

2009-06-22 09:23:18

事件监听器

2021-02-21 12:09:32

C 语言基础语法

2021-02-20 06:13:18

C 语言C++

2021-04-13 08:42:29

C语言数据类型转换自动类型转换

2021-02-11 08:25:17

C 语言C++ 基础

2021-02-16 10:57:34

C++ C 语言windows

2021-02-08 20:25:12

C 语言C++Linux

2009-07-24 09:20:15

数组实例

2011-04-15 09:20:56

ASP.NET MVC

2020-06-10 10:50:48

C++开发编程

2009-07-24 10:09:08

ASP.NET个性化ASP.NET基础教程
点赞
收藏

51CTO技术栈公众号