C++(11)提供了mem_fn,类似于std::function,用于将类的成员函数转换为函数对象:
#include <functional>
#include <iostream>
#include <string>
using namespace std;
class T{
public:
void pout(int d)
{
cout<<"this:"<<this<<" T::pout"<<" d="<<d<<endl;
}
};
int main()
{
auto f = mem_fn(&T::pout);
T t1;
f(t1, 8);
cout<<"t1 addr:"<<&t1<<endl;
return 0;
}
运行程序输出:
this:0x7ffdb27a7f8b T::pout d=8
t1 addr:0x7ffdb27a7f8b