C++11 function函數用法
阿新 • • 發佈:2018-08-20
ptr 指針 amp ber int() let fun bind ***
// C++11 function #include <functional> void print_num(int i) { std::cout << i << ‘\n‘; } void testFunctionTpl() { // templet 匿名函數 std::function<void(const int&, const int&)>getSum = [&](const int &nI1, const int &nI2) { int nSum = nI1 + nI2; std::cout << nSum << endl; }; getSum(8, 9); // lambda std::function<void()> f_display_42 = []() { print_num(42); }; f_display_42(); //********************* 測試成員函數 begin*********************// class TestClass { public: TestClass(int i) : m_nI(i){} ~TestClass() {} void testMemberFunc(int i) const { std::cout << m_nI + i << endl; } int testResultFun(int i) const { return m_nI + i; } void testBindFun(int num) const { std::cout << m_nI + num << endl; } int m_nI; }; std::function<void(const TestClass&, int)> testMemberFunc = &TestClass::testMemberFunc; const TestClass oTestObject(100); // 傳對象 testMemberFunc(oTestObject, 100); // 會根據模板構造對象 testMemberFunc(300, 100); // 返回值成員函數 std::function<int(const TestClass&, int)> testResultFun = &TestClass::testResultFun; int nRet = testResultFun(oTestObject, 100); std::cout << nRet << endl; // 成員變量 std::function<int(const TestClass&)> f_num = &TestClass::m_nI; std::cout << "m_nI: " << f_num(oTestObject) << ‘\n‘; // 對象形式調用成員函數 using std::placeholders::_1; std::function<void(int)> testBindFun = std::bind(&TestClass::testBindFun, oTestObject, _1); testBindFun(222); // 指針形式調用成員函數 std::function<void(int)> testBindFun2 = std::bind(&TestClass::testBindFun, &oTestObject, _1); testBindFun2(333); //********************* 測試成員函數 end*********************// //********************* 測試普通函數 begin*********************// // 綁定普通函數 std::function<void()> f_display_31337 = std::bind(print_num, 31337); f_display_31337(); //********************* 測試普通函數 end*********************// // bad_function_call std::function<int()> f = nullptr; try { f(); } catch (const std::bad_function_call& e) { std::cout << e.what() << ‘\n‘; } } int main() { testFunctionTpl(); int i = 0; }
C++11 function函數用法