使用std::bind繫結成員函式
阿新 • • 發佈:2019-01-24
在Cocos2d-x中看到CC_CALLBACK_2的巨集定義,繫結成員函式實現觸屏事件的處理,寫了一個測試程式碼模擬函式呼叫的過程。
// TestCpp.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <functional> #include <iostream> using namespace std; class Test { public: Test() {} ~Test() {} int getVal() { return m_val; } int add(int a, int b) { m_val = a + b; return m_val; } private: int m_val; }; int _tmain(int argc, _TCHAR* argv[]) { Test *t = new Test; auto func = std::bind(&Test::add,t,std::placeholders::_1,std::placeholders::_2); int val = func(1,1); cout << val << endl; delete t; t = NULL; getchar(); return 0; }