#include <iostream>
#include <functional>
using namespace std;
void func(int i, int g)
{
cout << i << " " << g <<endl;
}
void funcWapper(const std::function<void(int, int)>& func, int i, int g)//const must , or compiler error
{
func( i, g);
}
void funcWapperWithoutConst(std::function<void(int, int)>& func, int i, int g)
{
func( i, g);
}
void funcClassWapper(const std::function<void(int)>& func, int i)//const must , or compiler error
{
func(i);
}
class CB
{
public:
static void func(int i)
{
cout << "cb " << i <<endl;
}
void cb_func()
{
i = 1;
funcClassWapper(CB::func, 1);
}
int i = 0;
};
int main()
{
funcWapper(func, 10, 1);
//or
std::function<void(int, int)> job = func;
funcWapperWithoutConst(job, 10 , 1);
CB* cb = new CB();
cb->cb_func();
return 0;
}
C++ function 函数
于 2023-03-30 16:36:57 首次发布