1. 程式人生 > >郭立東的個人部落格

郭立東的個人部落格

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; }