实例代码
#include <iostream>
#include <string>
#include <vector>
#include <functional>
using namespace std;
void myfunc(int tv){
cout << "myfunc()函数执行了, tv" << tv << endl;
}
class TC{
public:
void operator()(int tv){
cout << "TC::operator() 执行了 tv = " << tv << endl;
}
};
class TC2{
public:
using tfpoint = void(*)(int);
static void myfunc(int tv){
cout << "TC2::myfunc() 静态成员函数执行了 tv = " << tv << endl;
}
operator tfpoint(){ return myfunc; }
};
class TC3{
public:
void operator()(int tv){
cout << "TC3::operator() 执行了 tv = " << tv << endl;
}
void ptfunc(int tv){
cout << "TC3::ptfunc() 执行了 tv = " << tv << endl;
}
static int stcfunc(int tv){
cout << "TC3::stcfunc()静态函数执行了, tv= " << tv << endl;
return tv;
}
int m_a;
};
class CB{
std::function<void()> fcallback;
public:
CB(const std::function<void()>& f) :fcallback(f){
int i = 1;
}
void runcallback(void){
fcallback();
}
};
class CT{
public:
void operator()(void){
cout << "operator()执行" << endl;
}
};
void mycallback(int cs, const std::function<void(int)> &f){
f(cs);
}
void runfunc(int x){
cout << x << endl;
}
void myfunc1(int x, int y, int z){
cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}
void myfunc2(int&x, int&y){
x++;
y++;
}
class CD{
public:
CD(){
cout << "CD构造函数" << endl;
}
CD(const CD&tm){
cout << "CD拷贝构造函数" << endl;
}
~CD(){
cout << "CD析构函数" << endl;
}
void myfunpt(int x, int y){
cout << "x = " << x << ", y = " << y << endl;
m_a = x;
}
int m_a = 0;
};
class CF{
public:
CF(){
cout << "CF构造函数" << endl;
}
CF(const CF&tm){
cout << "CF拷贝构造函数" << endl;
}
~CF(){
cout << "CF析构函数" << endl;
}
void operator()(){
cout << "operator()被调用" << this << endl;
}
int m_a = 0;
};
int main() {
void(*pmf)(int) = &myfunc;
pmf(15);
TC tc;
tc(20);
TC2 tc2;
tc2(50);
TC3 tc3;
void (TC3::*myfpointpt)(int) = &TC3::ptfunc;
(tc3.*myfpointpt)(68);
std::function<void(int)> f1 = myfunc;
f1(100);
std::function<int(int)> fs2 = TC3::stcfunc;
cout << fs2(110) << endl;
TC3 tc4;
std::function<void(int)> f4 = tc4;
f4(120);
TC2 tc5;
std::function<void(int)> f5 = tc5;
f5(150);
CT ct;
CB cb(ct);
cb.runcallback();
for (int i = 0; i < 10; ++i) {
mycallback(i, runfunc);
}
auto bf1 = std::bind(myfunc1, 10, 20, 30);
bf1();
auto bf2 = std::bind(myfunc1, placeholders::_1, placeholders::_2, 30);
bf2(5, 15);
auto bf3 = std::bind(myfunc1, placeholders::_1, placeholders::_1, 30);
bf3(5, 15);
std::bind(myfunc1, placeholders::_1, placeholders::_2, 30)(5, 15);
auto bf4 = std::bind(myfunc1, placeholders::_2, placeholders::_1, 30);
bf4(5, 15);
int a = 2;
int b = 3;
auto bf5 = std::bind(myfunc2, a, placeholders::_1);
bf5(b);
cout << "a = " << a << "b = " << b << endl;
CD cd;
auto bf6 = std::bind(&CD::myfunpt, cd, std::placeholders::_1, std::placeholders::_2);
bf6(10, 20);
auto bf7 = std::bind(&CD::myfunpt, &cd, std::placeholders::_1, std::placeholders::_2);
bf7(10, 20);
CD cd1;
std::function<void(int, int)> bfc6 = std::bind(&CD::myfunpt, cd1, std::placeholders::_1, std::placeholders::_2);
bfc6(10, 20);
std::function<int &()> bf9 = std::bind(&CD::m_a, cd1);
bf9() = 60;
auto rt = std::bind(CF());
rt();
auto bf10 = std::bind(runfunc, std::placeholders::_1);
for (int i = 0; i < 10; i++) {
mycallback(i, bf10);
}
system("pause");
return 0;
}