2012-05-25 46 views
1

我使用boost::signals2库,这里是简单的代码:信号和绑定参数

boost::signals2<void()> sig; 
class Foo { 
    void Slot() {} 
}; 

Foo obj; 
sig.connect(boost.bind(&Foo::Slot, &obj)); 

一切运作良好。但现在我想让我的信号通过参数中的某些数据:

boost::signals2<void (std::vector<float>)> sig2; 
class Foo { 
    void Slot2(std::vector<float>) {} 
}; 

绑定Slot2再次发信号的正确方法是什么?

这里是错误:http://dpaste.com/752076/当我使用相同的连接&的绑定代码。

回答

8

您需要使用的占位符:

sig.connect(boost::bind(&Foo::Slot, &obj, _1)); 
2

您需要阅读Automatic Connection Management,在一个类似的例子就如何连接插槽,接受参数存在。

在你的情况下,它是这样的:

#include <boost/signals2.hpp> 
#include <vector> 

struct Foo { 
    void Slot1() {} 
    void Slot2(std::vector<float>) {} 
}; 

int main() 
{ 
    typedef boost::signals2::signal<void()> st1; 
    typedef st1::slot_type sst1; 
    typedef boost::signals2::signal<void (std::vector<float>)> st2; 
    typedef st2::slot_type sst2; 

    st1 sig1; 
    st2 sig2; 

    Foo foo; 

    sig1.connect(sst1(&Foo::Slot1, foo)); 
    sig2.connect(sst2(&Foo::Slot2, foo, _1)); 

    sig1(); 
    std::vector<float> v(5,2.2); 
    sig2(v); 
}