C++11之函数对象

1、使用场景

在没有C++11的时候,我们通常使用回调函数来完成某些特定的功能,使用回调函数就需要先声明函数指针

示例:

typedef int(*PFunction)(int);

这里的 PFunction 就是一个函数指针类型,我们可以用它定义函数指针对象,传递到指定位置,用于回调

但C++11的 std::function 出现之后,想要实现上述功能,不再需要函数指针,使用更加简单

使用函数对象需要包含头文件:

#include <functional>

2、函数对象

函数对象通过重载 () 实现

示例:

class MyClass{
public:
	int operator()(int a){
        return a;
    }    
}

如上,MyClass 类中重载 () 符号,调用如下:

MyClass myClass;
myClass(12);

myClass 对象就是一个类函数对象

示例:

int TestFunction(int num){
    cout<<num<<endl;
}
int TestFunction2(int num){
    cout<<num+1<<endl;
}

std::function<int(int)> fun;
fun = TestFunction;
fun(22);//22
fun = TestFunction2;
fun(22);//23

如上,fun 对象就是一个函数对象,它的返回值是 int,需要一个 int 类型的参数

std::function 也可以用在 lambda 上

auto lambda = [](int num, const string& str) -> int {
    cout<<str<<endl;
    return num;
}
std::function<int(int, const string&)> func = lambda;
func(12, "aaa");

如上例可以看出,std::function 只是保存了函数,使用时仍然需要调用传参

如果希望传递类成员函数,可以使用STL提供的另一种方法: std::bind

3、std::bind

上面的 std::function 定义函数对象,但是没有绑定函数,也没有绑定参数,std::bind 方法返回一个函数对象,绑定参数和函数

示例:

void Function(int num, const string &str)
{
    cout << num << " " << str << endl;
}
auto bind = std::bind(Function, 2, "aa");
bind();//2 aa
bind(3, "cc", 11);//2 aa,不会报错,但是参数无效

//使用参数占位符
auto bind2 = std::bind(Function, placeholders::_1, placeholders::_2);
bind2(3, "cc");//3 cc

上例中,bind 对象绑定了函数 Function,以及两个参数,调用时不需要再次传参。也可以使用stl的占位符来预留参数位置

也可以使用成员函数,但是必须且传递的第一个参数必须是对象指针

示例:

class Test
{
public:
    Test(int num)
        : num(num)
    {}
    int Function(int n)
    {
        cout << num << endl;
        return num + n;
    }
private:
    int num;
};

int main()
{
    Test test(12);
    auto bind = std::bind(&Test::Function, &test, 9);
    auto res = bind();
    cout << res << endl;
    return 0;
}

输出

12
21

4、总结

C++11 的函数对象用在需要使用函数指针的地方,替代之

原文地址:https://www.cnblogs.com/sherlock-lin/p/15435310.html