阅读了一下cpluscplus中关于std::function的解释,写了测试程序进行验证
function - C++ Referencehttp://www.cplusplus.com/reference/functional/function/?kw=functiondemo程序如下:
#include <stdlib.h>
#include <stdio.h>
#include <functional>
// 1,
void Func_case1()
{
printf("Func_case1\n");
}
// 2,
typedef void(*Func_Pointer)();
Func_Pointer func_p = Func_case1;
// 3, 4,
class CA{
public:
CA() = default;
~CA() = default;
void member_func()
{
printf("call CA::member_func\n");
}
void operator()()
{
printf("call CA::operator()()\n");
}
};
int main()
{
// http://www.cplusplus.com/reference/functional/function/?kw=function
// An object of a function class instantiation can wrap any of the following kinds of callable objects:
// 1, a function,
// 2, a function pointer,
// 3, a pointer to member,
// 4, or any kind of function object (i.e., an object whose class defines operator(), including closures).
std::function<void()> func1;
// 1,
func1 = Func_case1;
func1();
// 2,
func1 = func_p;
func1();
CA a;
// 3,
std::function<void(CA*)> func2;
func2 = &CA::member_func;
func2(&a);
// 4,
func1 = a;
func1();
return 0;
}
以上的每种case用1,2,3,4进行标识
执行结果如下:
Func_case1
Func_case1
call CA::member_func
call CA::operator()()