std::function模板类是一个通用的可调用对象的包装器,用简单的、统一的方式处理可调用对象。
#include <iostream>
#include<functional>
using namespace std;
void show(int age, const string& message)
{
cout << "age " << age << "message " << message << endl;
}
class AA
{
public:
static void show(int age, const string& message)
{
cout << "age " << age << "message " << message << endl;
}
};
struct BB
{
void operator()(int age, const string& message)
{
cout << "age " << age << "message " << message << endl;
}
};
class CC
{
public:
void show(int age, const string& message)
{
cout << "age " << age << "message " << message << endl;
}
};
int main()
{
using Fun = void(int, const string&);
auto lb = [](int age, const string& message)
{
cout << "age " << age << "message " << message << endl;
};
function<void(int, const string&)> fn1 = show;
function<void(int, const string&)> fn2 = AA::show;
function<void(int, const string&)> fn3 = BB();
function<void(int, const string&)> fn4 = lb;
function<void(CC&, int, const string&)> fun5 = &CC::show;
fn1(1,"Name");
fn2(1, "Name");
fn3(1, "Name");
fn4(1, "Name");
CC cc;
fun5(cc,1,"Name");
}
标签:const,string,show,包装,对象,int,message,cout
From: https://blog.csdn.net/WNX10086/article/details/140562028