服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++类重载函数的function和bind使用示例

C++类重载函数的function和bind使用示例

2021-10-19 12:13huik C/C++

这篇文章主要介绍了C++类重载函数的function和bind使用示例,帮助大家更好的理解和使用c++,感兴趣的朋友可以了解下

在没有C++11的std::function和std::bind之前,我们使用函数指针的方式是五花八门,结构很繁琐难懂。C++11中提供了std::function和std::bind统一了可调用对象的各种操作。

1.std::function简介

std::function首先是可调用对象,本质上生成了一个类(仿函数)

简单的使用如下代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <unordered_map>
#include <iostream>
#include <functional>
using namespace std;
int func(int a)
{
  cout << a << __FUNCTION__ << endl;
  return a;
}
int main()
{
  using NewType = function<int(int)>; // function本质上生成了一个类(仿函数)
  NewType f1 = func;
  f1(55);
  return 0;
}

2.std::function与std::bind联合使用绑定类成员函数

可将std::bind函数看作一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。

std::bind将可调用对象与其参数一起进行绑定,绑定后的结果可以使用std::function保存。std::bind主要有以下两个作用:

  • 将可调用对象和其参数绑定成一个防函数;
  • 只绑定部分参数,减少可调用对象传入的参数。

一个简单的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <unordered_map>
#include <iostream>
#include <functional>
using namespace std;
class A
{
public:
  int funcA(int a)
  {
    cout << "111 " << a << endl;
    return 1;
  }
};
 
int main()
{
  A a;
  using NewType = function<int(int)>; // function本质上生成了一个类(仿函数)
  NewType f1 = bind(&A::funcA, &a, std::placeholders::_1);
  f1(55);
  return 0;
}

3.std::function与std::bind联合使用绑定类成员重载函数

绑定类成员重载函数的难点在于如何区分函数绑定的是哪一个成员函数。这时需要在函数指针前指定其类型。下面是一个简单的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <unordered_map>
#include <iostream>
#include <functional>
using namespace std;
class A
{
public:
  int funcA(int a)
  {
    cout << "111 " << a << endl;
    return 1;
  }
  int funcA(int a, int b)
  {
    cout << "222 " << a << endl;
    return a + b;
  }
};
 
int main()
{
  unordered_map<int, void *> funcMap;//尝试将其转载入map中
  A g;
  using NewType1 = function<int(int, int)>;
  using NewType2 = function<int(int)>;
  NewType1* type1 = new NewType1; // function本质上生成了一个类(仿函数)
  NewType2* type2 = new NewType2;
  //获取重载函数指针
  *type1 = std::bind((int(A::*)(int, int)) & A::funcA, &g, std::placeholders::_1, std::placeholders::_2);
  *type2 = std::bind((int(A::*)(int)) & A::funcA, &g, std::placeholders::_1);
  //
  funcMap[1] = type1; //
  funcMap[2] = type2; //
  
  // 使用
  void* s1 = funcMap[1];
  void* s2 = funcMap[2];
  NewType1* f1 = (NewType1*)(s1);
  NewType2* f2 = (NewType2*)(s2);
  (*f1)(1,5);
  (*f2)(55);
  return 0;
}

最近在工作中,遇到了需要编写大量重复代码的工作,于是尝试将这些重载函数放入映射中,只需要添加注册函数,最终可以统一使用映射表调用所需要的函数,function与bind方法给了我帮助,我也将代码分享给大家。

以上就是C++类重载函数的function和bind使用示例的详细内容,更多关于C++类重载函数的function和bind的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/huik-hub/p/13611315.html

延伸 · 阅读

精彩推荐