您的位置:首页 > 编程语言 > C语言/C++

C++回调函数示例

2006-04-06 12:19 295 查看
模板 + Boost::Function。示例代码:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: