菜鸟笔记
提升您的技术认知

c++之函数对象bind函数

函数对象实质上是一个实现了operator()--括号操作符--的类。

class Add
{
public:
    int operator()(int a, int b)
    {
        return a + b;
    }
};

int main()
{
    Add add; // 定义函数对象
    cout << add(3, 2); // 5

    system("pause");
    return 0;
}

函数指针版本就是:

int AddFunc(int a, int b)
{
    return a + b;
}
typedef int(*Add) (int a, int b);

int main()
{
    Add add = &AddFunc;
    cout << add(3, 2); // 5  //(*add)(3,2)

    system("pause");
    return 0;
}

既然函数对象与函数指针在使用方式上没什么区别,那为什么要用函数对象呢?很简单,函数对象可以携带附加数据,而指针就不行了。
下面就举个使用附加数据的例子:

class Less
{
public:
    Less(int num) :n(num) {}
    bool operator()(int value)
    {
        return value < n;
    }
private:
    int n;
};

int main()
{
    Less isLess(10);
    cout << isLess(9) << " " << isLess(12); // 输出 1 0

    system("pause");
    return 0;
}

 bind是这样一种机制,它可以预先把指定函数的某些参数绑定到已有的变量,产生一个新的函数,这种机制在回调函数的使用过程中也颇为有用。

int Func(int x, int y)
{
    return x + y;
}

int main()
{
    //bf1把拥有两个参数的普通函数的第一个参数绑定为10,生成了一个新的一个参数的函数
    auto bf1 = std::bind(Func, 10, std::placeholders::_1);
    cout<<bf1(20); ///< same as Func(10, 20)  

    system("pause");
    return 0;
}

 

class A
{
public:
    int Func(int x, int y)
    {
        return x + y;
    }
};


int main()
{
    A a;
    //bf2把一个类成员函数绑定了类对象,生成了一个像普通函数一样的新的可调用实体
    auto bf2 = std::bind(&A::Func, a, std::placeholders::_1, std::placeholders::_2);
    cout << bf2(10, 20); ///< same as a.Func(10, 20)  

    system("pause");
    return 0;
}

使用bind需要注意的一些事项:

  • (1)bind预先绑定的参数需要传具体的变量或值进去,对于预先绑定的参数,是pass-by-value的
  • (2)对于不事先绑定的参数,需要传std::placeholders进去,从_1开始,依次递增。placeholder是pass-by-reference的
  • (3)bind的返回值是可调用实体,可以直接赋给std::function对象
  • (4)对于绑定的指针、引用类型的参数,使用者需要保证在可调用实体调用之前,这些参数是可用的
  • (5)类的this可以通过对象或者指针来绑定

 

 

参考:http://blog.csdn.net/crayondeng/article/details/9996625