加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 将boost :: signal作为boost :: function传递

发布时间:2020-12-16 09:28:51 所属栏目:百科 来源:网络整理
导读:我有一个带有信号成员的类,用boost :: function封装. 是否可以使用此API添加另一个信号作为处理程序? class Foo{public: VOID AddHandler(boost::functionVOID() handler) { m_signal.connect(handler); }private: boost::signalVOID() m_signal;};boost::s
我有一个带有信号成员的类,用boost :: function封装.

是否可以使用此API添加另一个信号作为处理程序?

class Foo
{
public:
  VOID AddHandler(boost::function<VOID()> handler)
  {
     m_signal.connect(handler);
  }

private:
  boost::signal<VOID()> m_signal;
};

boost::signal<VOID()> signal;

VOID SignalCaller()
{
    signal();
}

int main( )
{ 
   Foo foo;
   //foo.AddHandler(signal); // I want to
   foo.AddHandler(&SignalCaller); // I have to
}

解决方法

使用在信号类型中声明的类型“slot_type”

class Foo
{
public:
    typedef boost::signal0<void> Signal;
    typedef Signal::slot_type Slot;

    //allowed any handler type which is convertible to Slot
    void AddHandler(Slot handler)
    {
        m_signal.connect(handler);
    }
private:
  Signal m_signal;
};

void f()
{
    std::cout << "f() called";
}

//usage
    Foo foo;
    foo.AddHandler(signal);
    foo.AddHandler(&f);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读