C++中的function

1 #include<iostream>
2 #include<functional>

学习C++中的function,记下,免得忘了。

更详细的可以看这里

 1 int powerFunc(int x)
 2 {
 3     return x << 1;
 4 }
 5 struct test
 6 {
 7 public:
 8     test(int aa) :m_test(aa) {}
 9     ~test() {}
10     int RetMemValue(int offset)
11     {
12         return m_test << offset;
13     }
14 private:
15     int m_test;
16 };
17 struct OutValue
18 {
19     void operator()(int value,int offset) const
20     {
21         cout << (value << offset) << endl;
22     }
23 };

int    main(int argc, char **argv)
{
    //Free function
    std::function<int(int)> p_power = powerFunc;
    cout << p_power(5) << endl;
    //lambda
    std::function<int()> p_lambda = []() {return powerFunc(6); };
    cout << p_lambda() << endl;
    //bind
    std::function<int()> p_bind = std::bind(powerFunc, 7);
    cout << p_bind() << endl;
    //mem function
    std::function<int(test&,int)> p_memFunc = &test::RetMemValue;
    test obj_test(8);
    cout << p_memFunc(obj_test, 2) << endl;
  //function obj
    std::function<void(int, int)> p_memAndValue = OutValue();
    p_memAndValue(9, 2);
}

 

posted on 2016-03-24 14:12  真.新手上路  阅读(625)  评论(0)    收藏  举报

导航