我想做一个串口事件回调函数,当串口CTS线状态变化时,调用一个指定的处理函数,有没有例子呀,不是串口的也行,只要是这种回调机制

解决方案 »

  1.   

    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;
    }
      

  2.   

    如何用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;
    }
      

  3.   


    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;
    }
    };
      

  4.   

    #include "boost/signals.hpp"
    怎么VC编译不通过,这是个什么类,文件在那个目录,怎么才能引用,是不是要包含某个库?
      

  5.   

    boost是比较优秀的标准C++库,www.boost.org
      

  6.   

    方法很多
    1.使用标准C的做法
    typedef int (__cdecl * MyFunction) ();static int __MyFunction()
    {
      return 0;
    }void TestInvoke(MyFunction myf)
    {
     myf();
    }
    void main()
    {
     MyFunction testInvoke = __MyFunction;
    TestInvoke(testInvoke )
     
    }
      

  7.   

    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>
    就可以了
      

  8.   

    我想学学boost,请问去哪里找资料?
    另外VC安装时是不是不带Boost的,我怎么搜索不到那些文件?
      

  9.   

    问题
    回不回调,看你什么看,只要能达到你的要求就行
    第二种方法,也可以完成你的要求
    客户实现的不再是一个函数,而是一个对象.这样做在某些情况也更合理一些boost的本不多.你可以到书店去卖应当还是有的
    到www.boost.org去下载
      

  10.   

    指针回调,是并不安全的,建议使用对象回调,boost::signal可以自动维护对象生命周期,不会因为回调对象销毁导致程序异常
      

  11.   

    boost::signal教程译本:http://blog.csdn.net/jq0123/archive/2008/06/30/2598384.aspx
    boost::thread教程: http://blog.csdn.net/IamNieo/archive/2008/09/10/2908621.aspx