如何实现自己的回调函数? 我想做一个串口事件回调函数,当串口CTS线状态变化时,调用一个指定的处理函数,有没有例子呀,不是串口的也行,只要是这种回调机制 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 boost::signal是比较先进的回调机制可以完全实现调用者之间的无缝耦合#include <iostream>#include "boost/signals.hpp"using namespace std;void _slot(){ cout << "SLOT" << __FILE__ << ":" << __LINE__ << endl;}class class1{public: void operator()() const { cout << "class1:operator()" << endl; }};int _tmain(int argc, _TCHAR* argv[]){ boost::signal<void()> sig; sig.connect(&_slot); sig.connect(class1()); cout << "emitting one signal..." << endl; sig(); return 0;} 如何用boost::signal实现应用回调#include <iostream>#include <string>#include <vector>#include <algorithm>#include <boost/signals.hpp>#include <boost/function.hpp>#include <boost/bind.hpp>using namespace std;class Instance{public: typedef boost::signal<void(void)> _CallbackStart; typedef boost::signal<void(int)> _CallbackChannelSelf;protected: _CallbackStart sigStart_; _CallbackChannelSelf sigChannelSelf_;public: void Start() { sigStart_(); } void ChannelSelf(int channel) { sigChannelSelf_(channel); } void RegisterStartCallback(const _CallbackStart::slot_type & slot) { sigStart_.connect(slot); } void RegisterChannelCallback(const _CallbackChannelSelf::slot_type & slot) { sigChannelSelf_.connect(slot); }};void CallbackStart(){ cout << "f started" << endl;}int _tmain(int argc, _TCHAR* argv[]){ Instance ins; app myapp; ins.RegisterStartCallback(CallbackStart); ins.RegisterStartCallback(boost::bind(&app::onInstanceStart,&myapp)); ins.RegisterChannelCallback(boost::bind(&app::onChannelSelf,&myapp,_1)); ins.Start(); ins.ChannelSelf(123); return 0;} class app{public: app() : name_("my app") { } string name_;public: void onInstanceStart() { cout << "app[" << name_ << "] instance started" << endl; } void onChannelSelf(int channel) { cout << "channel " << channel << " started." << endl; }}; #include "boost/signals.hpp"怎么VC编译不通过,这是个什么类,文件在那个目录,怎么才能引用,是不是要包含某个库? boost是比较优秀的标准C++库,www.boost.org 方法很多1.使用标准C的做法typedef int (__cdecl * MyFunction) ();static int __MyFunction(){ return 0;}void TestInvoke(MyFunction myf){ myf();}void main(){ MyFunction testInvoke = __MyFunction;TestInvoke(testInvoke ) } 2.也可以使用类对象class callback{public:virtual void call();};void TestInvoke(callback &cb){cb.call();}void main(){callback cb;TestInvoke(cb)}3.当然也可以使用boost方法,boost这方面做的还是比较可以的.signals是对于发布,订阅模式的,你只需使用#include <boost/function.hpp>#include <boost/bind.hpp>就可以了 我想学学boost,请问去哪里找资料?另外VC安装时是不是不带Boost的,我怎么搜索不到那些文件? 问题回不回调,看你什么看,只要能达到你的要求就行第二种方法,也可以完成你的要求客户实现的不再是一个函数,而是一个对象.这样做在某些情况也更合理一些boost的本不多.你可以到书店去卖应当还是有的到www.boost.org去下载 指针回调,是并不安全的,建议使用对象回调,boost::signal可以自动维护对象生命周期,不会因为回调对象销毁导致程序异常 boost::signal教程译本:http://blog.csdn.net/jq0123/archive/2008/06/30/2598384.aspxboost::thread教程: http://blog.csdn.net/IamNieo/archive/2008/09/10/2908621.aspx 想做个批量删帖机,但以前没接触过这方面,该看那些书先? 急啊!!!!用vc来模拟实现鼠标右键点击IE,选属性中的“Internet 选项。。。”,然后再点击确定的过程!! 关于字体的问题? CString 如何取子串?返回CString 未入门新手求助! 关于HTTP请求头重新开个贴子 怎么我解决不了的问题都是大家解决不了的问题,高分诚邀高手,不够再加。 定时器的取消,急啊 请教在vc中如何关联两个表进行查询 请问什么地方有VC的编程规范呀 用CSerialport类怎么将十进制整数按照16进制格式发出去? 请教,MFC中 app类InitInstance函数中创建窗口对象的问题
可以完全实现调用者之间的无缝耦合
#include <iostream>
#include "boost/signals.hpp"
using namespace std;void _slot()
{
cout << "SLOT" << __FILE__ << ":" << __LINE__ << endl;
}
class class1
{
public:
void operator()() const
{
cout << "class1:operator()" << endl;
}};int _tmain(int argc, _TCHAR* argv[])
{
boost::signal<void()> sig;
sig.connect(&_slot);
sig.connect(class1());
cout << "emitting one signal..." << endl;
sig();
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/signals.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
using namespace std;
class Instance
{
public:
typedef boost::signal<void(void)> _CallbackStart;
typedef boost::signal<void(int)> _CallbackChannelSelf;
protected:
_CallbackStart sigStart_;
_CallbackChannelSelf sigChannelSelf_;
public:
void Start()
{
sigStart_();
}
void ChannelSelf(int channel)
{
sigChannelSelf_(channel);
}
void RegisterStartCallback(const _CallbackStart::slot_type & slot)
{
sigStart_.connect(slot);
}
void RegisterChannelCallback(const _CallbackChannelSelf::slot_type & slot)
{
sigChannelSelf_.connect(slot);
}
};void CallbackStart()
{
cout << "f started" << endl;
}int _tmain(int argc, _TCHAR* argv[])
{
Instance ins;
app myapp;
ins.RegisterStartCallback(CallbackStart);
ins.RegisterStartCallback(boost::bind(&app::onInstanceStart,&myapp));
ins.RegisterChannelCallback(boost::bind(&app::onChannelSelf,&myapp,_1));
ins.Start();
ins.ChannelSelf(123);
return 0;
}
class app
{
public:
app()
: name_("my app")
{
}
string name_;
public:
void onInstanceStart()
{
cout << "app[" << name_ << "] instance started" << endl;
}
void onChannelSelf(int channel)
{
cout << "channel " << channel << " started." << endl;
}
};
怎么VC编译不通过,这是个什么类,文件在那个目录,怎么才能引用,是不是要包含某个库?
1.使用标准C的做法
typedef int (__cdecl * MyFunction) ();static int __MyFunction()
{
return 0;
}void TestInvoke(MyFunction myf)
{
myf();
}
void main()
{
MyFunction testInvoke = __MyFunction;
TestInvoke(testInvoke )
}
class callback
{
public:
virtual void call();
};void TestInvoke(callback &cb)
{
cb.call();
}
void main()
{
callback cb;
TestInvoke(cb)
}3.当然也可以使用boost方法,
boost这方面做的还是比较可以的.
signals是对于发布,订阅模式的,你只需使用
#include <boost/function.hpp>
#include <boost/bind.hpp>
就可以了
另外VC安装时是不是不带Boost的,我怎么搜索不到那些文件?
回不回调,看你什么看,只要能达到你的要求就行
第二种方法,也可以完成你的要求
客户实现的不再是一个函数,而是一个对象.这样做在某些情况也更合理一些boost的本不多.你可以到书店去卖应当还是有的
到www.boost.org去下载
boost::thread教程: http://blog.csdn.net/IamNieo/archive/2008/09/10/2908621.aspx