1. 程式人生 > >c++11新特性,類方法作為函式指標

c++11新特性,類方法作為函式指標

#include <functional>
#include <iostream>

using namespace std;

std::function<int (int)> Functional;
std::function<int (int, int)> FuncIntInt;
std::function<void ()>   FuncPt;

class Functor
{
  private:
    int member;
  public:
    int set_intint(int member, int beshow){
        cout << "I'm " << this->member << endl;
        this->member = member;
        cout << "I'm " << this->member << "beshow" << beshow  << endl;
        return 0;
    }
    int set_member(int member){
        cout << "I'm " << this->member << endl;
        this->member = member;
        cout << "I'm " << this->member << endl;
        return 0;
    }
    void pt_member(){
        cout << "I'm " << member << endl;
    }
};


int main(int argc, char *argv[])
{
    Functor test;

    Functional = std::bind(&Functor::set_member, test, std::placeholders::_1);
    Functional(666);
    Functional(888);

    FuncIntInt = std::bind(&Functor::set_intint, test, std::placeholders::_1, std::placeholders::_2);
    FuncIntInt(666, 888);
        
    FuncPt = std::bind(&Functor::pt_member, test);
    FuncPt();

    return 0;
}