包装普通函数
- #include
- #include
- #include
- using namespace std;
- int Max(int a, int b)
- {
- return a > b ? a : b;
- }
- void print()
- {
- cout << "无参无返回值" << endl;
- }
- int main()
- {
- function<int(int, int)> funMax(Max);
- cout << funMax(1, 2) << endl;
- function
funPrint(print); - print();
- printData(funMax, 1, 2);
- return 0;
- }
包装类的静态方法
- #include
- #include
- #include
- using namespace std;
- class Test
- {
- public:
- static void print(int a, int b)
- {
- cout << a + b << endl;
- }
- void operator()(string str)
- {
- cout << str << endl;
- }
- operator FuncPTR()
- {
- return print;
- }
- };
- int main()
- {
- //包装类的静态方法
- function
int, int)> sFunc = Test::print; - sFunc(1, 2);
- return 0;
- }
包装仿函数
- #include
- #include
- #include
- using namespace std;
- class Test
- {
- public:
- void operator()(string str)
- {
- cout << str << endl;
- }
- };
- int main()
- {
- //包装仿函数
- Test test;
- function
funTest = test; - test("仿函数");
- return 0;
- }
包装转换成函数指针的对象 (operator的隐式转换)
- #include
- #include
- #include
- using namespace std;
- using FuncPTR = void(*)(int, int);
- class Test
- {
- public:
- static void print(int a, int b)
- {
- cout << a + b << endl;
- }
- operator FuncPTR()
- {
- return print;
- }
- };
- int main()
- {
- //包装转换成函数指针的对象 (operator的隐式转换)
- Test object;
- function
int,int)> funOPE = object; - funOPE(2, 3);
- return 0;
- }