C++ 仿函数

//---------------------------------------------------

 

#include < functional>  

   

int Func(int x, int y);  

auto bf1 = std::bind(Func, 10, std::placeholders::_1);  

bf1(20); ///< same as Func(10, 20)  

   

class A  

{  

public:  

10     int Func(int x, int y);  

11 };  

12    

13 A a;  

14 auto bf2 = std::bind(&A::Func, a, std::placeholders::_1, std::placeholders::_2);  

15 bf2(10, 20); ///< same as a.Func(10, 20)  

16    

17 std::function< int(int)> bf3 = std::bind(&A::Func, a, std::placeholders::_1, 100);  

18 bf3(10); ///< same as a.Func(10, 100)

19 

 

//-----------------------------------------------------------

std::function与std::bind 函数指针

function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类 的非静态成员函数时。

std::function可以绑定到全局函数/类静态成员函数(类静态成员函数与全局函数没有区别),如果要绑定到类的非静态成员函数,则需要使用std::bind

 

[cpp] view plaincopy

 

#include <iostream>  

#include <functional>  

using namespace std;  

  

typedef std::function<void ()> fp;  

void g_fun()  

{  

    cout<<"g_fun()"<<endl;  

}  

10 class A  

11 {  

12 public:  

13     static void A_fun_static()  

14     {  

15         cout<<"A_fun_static()"<<endl;  

16     }  

17     void A_fun()  

18     {  

19         cout<<"A_fun()"<<endl;  

20     }  

21     void A_fun_int(int i)  

22     {  

23         cout<<"A_fun_int() "<<i<<endl;  

24     }  

25   

26     //非静态类成员,因为含有this指针,所以需要使用bind  

27     void init()  

28     {  

29         fp fp1=std::bind(&A::A_fun,this);  

30         fp1();  

31     }  

32   

33     void init2()  

34     {  

35         typedef std::function<void (int)> fpi;  

36         //对于参数要使用占位符 std::placeholders::_1  

37         fpi f=std::bind(&A::A_fun_int,this,std::placeholders::_1);  

38         f(5);  

39     }  

40 };  

41 int main()  

42 {  

43     //绑定到全局函数  

44     fp f2=fp(&g_fun);  

45     f2();  

46   

47     //绑定到类静态成员函数  

48     fp f1=fp(&A::A_fun_static);  

49     f1();  

50   

51     A().init();  

52     A().init2();  

53     return 0;  

54 }  

 

同时,std::bind绑定到虚函数时会表现出多态行为。

 

[cpp] view plaincopy

 

55 #include <iostream>  

56 #include <functional>  

57 using namespace std;  

58   

59 typedef std::function<void ()> fp;  

60   

61 class A  

62 {  

63 public:  

64     virtual void f()  

65     {  

66         cout<<"A::f()"<<endl;  

67     }  

68   

69     void init()  

70     {  

71         //std::bind可以表现出多态行为  

72         fp f=std::bind(&A::f,this);  

73         f();  

74     }  

75 };  

76 class B:public A  

77 {  

78 public:  

79     virtual void f()  

80     {  

81         cout<<"B::f()"<<endl;  

82     }  

83 };  

84 int main()  

85 {  

86     A* pa=new B;  

87     pa->init();  

88   

89     return 0;  

90 }  

20   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值