#include
using namespace std;void fun(int v)
{cout << "fun()函数执行了,v = " << v << endl;
}int main()
{void(*pfun)(int) = fun;pfun(15);return 0;
}
仿函数是一个重载了 operator() 运算符、能行使函数功能的类,这个类也称为函数对象类,这个类的对象就是函数对象。
函数对象本质上是一个对象,但其使用形式看起来和函数调用一样,因而得名。
#include
using namespace std;class TC
{
public:void operator()(int tv) // 函数调用运算符{cout << "TC::operator()执行了,tv = " << tv << endl;}
};int main()
{TC tc;tc(20); // 等价于tc.operator()(20);return 0;
}
#include
using namespace std;class TC
{
public:using pfun = void(*)(int); // 使用using指定别名static void fun(int tv) // 静态成员函数{cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;}operator pfun() // 类型转换运算符,能把本类类型对象转换成一个函数指针{return fun;}
};int main()
{TC tc;tc(50); // 等价于tc.operator TC::pfun()(50);return 0;
}
#include
using namespace std;class TC
{
public:void fun(int tv){cout << "TC::fun()非静态成员函数执行了,tv = " << tv << endl;}
};int main()
{TC tc;void(TC::*pfun)(int) = &TC::fun; // 类成员函数指针变量pfun的定义与初始化(tc.*pfun)(50); // 对象tc通过类成员函数指针pfun调用成员函数fun()return 0;
}
头文件为 #include
从上面的讲解中,我们可以发现:可调用对象的定义方法五花八门,保存和传递的过程很繁琐。为此,C++11 提供了解决方案,即可调用对象包装器 std::function。
std::function 是一个类模板,通过给它指定模板参数,它就能够用统一的方式来处理各种可调用对象,方便统一的保存和调用。
std::function 的应用场景:回调函数和作为函数参数。
#include
#include
using namespace std;void fun(int v)
{cout << "fun()函数执行了,v = " << v << endl;
}int main()
{std::function myf = fun;myf(100);return 0;
}
#include
#include
using namespace std;void fun(int v)
{cout << "fun()函数执行了,v = " << v << endl;
}int main()
{void(*pfun)(int) = fun;std::function myf = pfun;myf(15);return 0;
}
#include
#include
using namespace std;class TC
{
public:void operator()(int tv) // 函数调用运算符{cout << "TC::operator()执行了,tv = " << tv << endl;}
};int main()
{TC tc;std::function myf = tc;myf(100);return 0;
}
#include
#include
using namespace std;class TC
{
public:using pfun = void(*)(int); // 使用using指定别名static void fun(int tv) // 静态成员函数{cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;}operator pfun() // 类型转换运算符,能把本类类型对象转换成一个函数指针{return fun;}
};int main()
{TC tc;std::function myf = tc;myf(100);return 0;
}
#include
#include
using namespace std;class TC
{
public:static int fun(int tv) // 静态成员函数{cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;return tv;}
};int main()
{// 静态成员函数不包含隐藏的this指针std::function myf = TC::fun;cout << myf(100) << endl;return 0;
}
#include
#include
using namespace std;class TC
{
public:int fun(int tv) // 非静态成员函数{cout << "TC::fun()非静态成员函数执行了,tv = " << tv << endl;return tv;}
};int main()
{// 非静态成员函数包含一个隐藏的this指针,所以形参需要多定义一个类型std::function myf = &TC::fun;cout << myf(TC(), 100) << endl;return 0;
}
头文件:#include
std::bind() 是一个函数模板,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。具体说来,std::bind() 用于将可调用对象及其参数绑定到一起,返回一个仿函数,因此可以直接调用,也可以使用 std::function 进行保存。
std::bind() 的思想实际上是一种延迟计算的思想,将可调用对象保存起来,然后在需要的时候再调用,而且这种绑定是非常灵活的,不论是普通函数、函数对象、还是成员函数都可以绑定,而且其参数可以支持占位符。
#include
#include
using namespace std;void fun(int x, int y, int z)
{cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}int main()
{auto bf = std::bind(fun, 10, 20, 30);bf();return 0;
}
如果函数有多个参数,可以绑定部分参数,其他的参数在调用的时候指定。
#include
#include
using namespace std;void fun(int x, int y, int z)
{cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}int main()
{auto bf = std::bind(fun, placeholders::_1, 20, placeholders::_2);bf(10, 30);return 0;
}
#include
#include
using namespace std;void fun(int x, int y, int z)
{cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}int main()
{std::bind(fun, placeholders::_1, 20, placeholders::_1)(10, 30);return 0;
}
需要注意的是,std::bind() 对于预先绑定的函数参数是通过值传递的,对于通过 placeholders 占位的参数是通过引用传递的。
#include
#include
using namespace std;void fun(int& x, int& y)
{x++;y++;
}int main()
{int a = 2;int b = 3;auto bf = std::bind(fun, a, placeholders::_1);bf(b);cout << "a = " << a << ", b = " << b << endl;return 0;
}
3.3 绑定函数对象
#include
#include
using namespace std;class CQ
{
public:CQ(){cout << "构造函数 - " << this << endl;}CQ(const CQ&){cout << "拷贝构造函数 - " << this << endl;}~CQ(){cout << "析构函数 - " << this << endl;}void operator()() // 函数调用运算符{cout << "operator() - " << this << endl;}
};int main()
{auto bf = std::bind(CQ());bf();return 0;
}
在下面代码中,第二个参数为 cq,这会导致调用 CQ 类的拷贝构造函数生成一个 CQ 类型的临时对象作为 std::bind() 的返回值。
#include
#include
using namespace std;class CQ
{
public:CQ(){cout << "构造函数 - " << this << endl;}CQ(const CQ&){cout << "拷贝构造函数 - " << this << endl;}~CQ(){cout << "析构函数 - " << this << endl;}void fun(int x, int y){cout << "x = " << x << ", y = " << y << endl;}
};int main()
{CQ cq;auto bf = std::bind(&CQ::fun, cq, std::placeholders::_1, std::placeholders::_2);bf(10, 30);return 0;
}
在下面代码中,第二个参数为 &cq,这样就不生成 CQ 类型的临时对象了,此时 std::bind() 返回的就是 cq 对象本身。
#include
#include
using namespace std;class CQ
{
public:CQ(){cout << "构造函数 - " << this << endl;}CQ(const CQ&){cout << "拷贝构造函数 - " << this << endl;}~CQ(){cout << "析构函数 - " << this << endl;}void fun(int x, int y){cout << "x = " << x << ", y = " << y << endl;}
};int main()
{CQ cq;auto bf = std::bind(&CQ::fun, &cq, std::placeholders::_1, std::placeholders::_2);bf(10, 30);return 0;
}
#include
#include
using namespace std;class CQ
{
public:CQ(){cout << "构造函数 - " << this << endl;}CQ(const CQ&){cout << "拷贝构造函数 - " << this << endl;}~CQ(){cout << "析构函数 - " << this << endl;}void fun(int x, int y){cout << "x = " << x << ", y = " << y << endl;}
};int main()
{CQ cq;std::function bf = std::bind(&CQ::fun, cq, std::placeholders::_1, std::placeholders::_2);bf(10, 30);return 0;
}
原文链接:https://blog.csdn.net/qq_42815188/article/details/128056305