C++回調函數代碼示例解讀
作者:佚名
我們今天將會通過對一段代碼示例的解讀詳細了解一下C++回調函數的應用技巧。大家可以以此為參考對象,實踐操作一回,以對這一應用技術有一個深刻的認識。
C++回調函數的應用在實際程序開發中是一個比較常用的操作,那么對于我們初學者來說,應該如何正確,快速的掌握這一應用技巧呢?在這里就先來通過以下這段代碼的解讀來詳細了解下這方面的應用技巧。
C++回調函數代碼示例:
- #include < string>
- #include < iostream>
- #include < boost/function.hpp>
- using namespace std;
- using namespace boost;
- class Test
- {
- public:
- Test(){};
- virtual ~Test(){};
- void Handle(string& s, unsigned int lines)
- {
- for(int i=0; i< lines; i++)
- {
- cout < < s < < endl;
- }
- };
- };
- template < class T>
- static void CallBack(T& t, boost::function< void
(T*, string&, unsigned int)> f)- {
- string s("test");
- f(&t, s, 3);
- };
- int main()
- {
- Test test;
- CallBack< Test>(test, &Test::Handle);
- return 0;
- }
以上就是對C++回調函數的相關介紹。
【編輯推薦】
責任編輯:曹凱
來源:
博客園