021可调用对象
#include
#include
using namespace std;
void myFunciton(int value)
{
cout << "myFunciton函数执行了" << endl;
}
class TESTCLASS
{
public:
void operator()(int value)
{
cout << "这是类中重载了()运算符的函数执行了" << endl;
}
void myfunction02(int value)
{
cout << "testclass 类中的myfunction02函数执行了" << endl;
}
public:
int m_a;
};
class testclass02
{
public:
using tfpoint = void(*)(int);
static void myStaticFunction(int value)
{
cout << "testclass02静态成员函数执行了" << endl;
}
operator tfpoint()
{
return myStaticFunction;
}
};
int main(void)
{
void(*pMyFunc)(int) = myFunciton;
pMyFunc(23);
TESTCLASS ts;
ts.operator()(3);
ts(23);
testclass02 tc2;
tc2(40);
tc2.operator testclass02::tfpoint()(40);
TESTCLASS tc03;
void (TESTCLASS::*myfPoint)(int) = &TESTCLASS::myfunction02;
(tc03.*myfPoint)(234);
system("pause");
return 0;
}
022function()可调用对象包装器
#include
#include
#include
using namespace std;
void myFunciton(int value)
{
cout << "myFunciton函数执行了" << endl;
}
class TC
{
public:
void operator()(int value)
{
cout << "这是类中重载了()运算符的函数执行了" << endl;
}
void myfunction02(int value)
{
cout << "testclass 类中的myfunction02函数执行了" << endl;
}
static int stcfunc(int t)
{
cout << "类tc中的静态函数执行" << endl;
return t;
}
public:
int m_a=0;
};
class testclass02
{
public:
using tfpoint = void(*)(int);
static void myStaticFunction(int value)
{
cout << "testclass02静态成员函数执行了" << endl;
}
operator tfpoint()
{
return myStaticFunction;
}
};
int main(void)
{
std::function<void(int)> f1 = myFunciton;
f1(123);
std::function<int(int)>f2 = TC::stcfunc;
cout << f2(234) << endl;
TC tc03;
std::function<void(int)>f3 = tc03;
f3(23);
testclass02 tc02;
std::function<void(int)>f4 = tc02;
f4(23);
system("pause");
return 0;
}
023function()可调用对象包装器_范例演示1
#include
#include
#include
using namespace std;
void myFunciton(int value)
{
cout << "myFunciton函数执行了" << endl;
}
class CB
{
private:
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 << "CT类operator函数执行" << endl;
}
};
int main(void)
{
CT ct;
CB cb(ct);
cb.runCallBack();
system("pause");
return 0;
}
024function()可调用对象包装器_范例演示2
#include
#include
#include
using namespace std;
void myCallBack(int value,const std::function<void(int)>&f)
{
f(value);
}
void runfunction(int value)
{
cout << "value=" << value << endl;
}
int main(void)
{
for (int i=0;i<10;++i)
{
myCallBack(i, runfunction);
}
system("pause");
return 0;
}
025std_bind绑定器
#include
#include
#include
using namespace std;
void myFunction(int x,int y,int z)
{
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout << "z=" << z << endl;
}
void myFunction02(int x, int y, int z)
{
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout << "z=" << z << endl;
}
void myFunction03(int&x,int&y)
{
x++;
y++;
}
int main(void)
{
auto bf1 = std::bind(myFunction, 1, 2, 3);
bf1();
auto bf2 = std::bind(myFunction02, placeholders::_1, placeholders::_2, 34);
bf2(1, 2);
std::bind(myFunction02, placeholders::_1, placeholders::_2, 34)(12, 23);
auto bf3 = std::bind(myFunction02, placeholders::_2, placeholders::_1, 300);
bf3(1, 2);
int a = 1;
int b = 2;
auto bf4 = std::bind(myFunction03, a, placeholders::_1);
bf4(b);
cout << "a=" <<a<< endl;
cout << "b=" <<b<< endl;
system("pause");
return 0;
}
026std_bind绑定器_绑定成员函数_第二个参数是类对象
#include
#include
#include
using namespace std;
class CT
{
public:
void myFunction(int x,int y)
{
cout << "x=" << x << endl;
cout << "y=" << y << endl;
m_a = x;
}
public:
int m_a = 0;
};
int main(void)
{
CT ct;
auto bf1 = std::bind(&CT::myFunction, ct,std::placeholders::_1, std::placeholders::_2);
bf1(1, 2);
auto bf2 = std::bind(&CT::myFunction, &ct, std::placeholders::_1, std::placeholders::_2);
bf2(1, 2);
system("pause");
return 0;
}
027std_bind绑定器和function配合使用
#include
#include
#include
using namespace std;
class CT
{
public:
void myFunction(int x, int y)
{
cout << "x=" << x << endl;
cout << "y=" << y << endl;
m_a = x;
}
void operator()()
{
cout << "operator()函数被调用" << endl;
}
public:
int m_a = 0;
};
void myCallBack(int cs,const std::function<void(int)>&f)
{
f(cs);
}
void runFunc(int x)
{
cout << "runFunc函数执行了" <<x<< endl;
}
int main(void)
{
CT ct;
std::function<void(int, int)> bf1 = std::bind(&CT::myFunction, &ct, std::placeholders::_1, std::placeholders::_2);
bf1(10, 20);
std::function<int&()>bf2 = std::bind(&CT::m_a, &ct);
bf2() = 53;
auto rt = std::bind(CT());
auto bf4 = std::bind(runFunc, std::placeholders::_1);
for (int i=0;i<10;i++)
{
myCallBack(i, bf4);
}
system("pause");
return 0;
}