c++ bind functional

#include
#include <memory>
#include <functional>
#include <vector>
#include "Test.h"


using namespace std;

int test(int a, int b)
{
    cout << a << b << endl;
    return 10;
}


int main()
{
    //CTest test;
    //CTest *test = new CTest;
    //std::shared_ptr<CTest> test(new CTest);
    //std::unique_ptr<CTest>test(new CTest);

    //auto f = std::bind(&CTest::Show1, test,std::placeholders::_1,std::placeholders::_2);
    //auto f = std::bind(&CTest::Show1, std::ref(test), std::placeholders::_1, std::placeholders::_2);
    //f(10,20);

    //auto f = std::bind(test, 10, std::placeholders::_1);
    //f(20);

    /*
    std::vector<std::shared_ptr<CTest>> mVec;
    for (int i = 0; i < 10; ++i)
    {
        mVec.push_back( std::shared_ptr<CTest>(new CTest) );
    }

    
    for_each(begin(mVec), end(mVec), [](std::shared_ptr<CTest> pTest)
    {
        auto f = std::bind(&CTest::Show1, pTest, 10, std::placeholders::_1);
        f(20);
    }
    );
    

    for_each(mVec.begin(), mVec.end(), std::bind(&CTest::Show1, std::placeholders::_1, 10, 20));
    */

    std::function<int(int, int)> fu = std::bind(test, std::placeholders::_1, std::placeholders::_1);
    fu(10,20);

    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章