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

猜你喜欢

转载自blog.csdn.net/u010029439/article/details/82221162
今日推荐