郭立東的個人部落格
阿新 • • 發佈:2019-01-31
C++98
ptr_fun
函式指標 -> 仿函式
int sum ( int a , int b)
{
return a+b;
}
auto sum_obj = std::ptr_fun(sum);
mem_fn
成員函式地址 -> 仿函式
class test
{
public :
int sum(int a, int b){ return a+b ; }
};
auto sum_obj = std::mem_fn(&test::sum);
C++11
#include <functional>
#include <iostream>
class Test{
public:
Test( int i) : m_int(i) {}
void Print() {
std::cout<<m_int<<std::endl;
}
private:
int m_int;
};
int main(){
Test t1(1);
Test t2(2);
auto mem_fn = std::mem_fn(&Test::Print);
auto fn = std::bind(&Test::Print, &t1);
fn();
mem_fn(t2);
return 0;
}