![](/img/trans.png)
[英]boost::signal2 bind to pure or member function using enable_if
[英]How to connect a boost::signal2::signal to a pure virtual function?
我在组件UpdateComponent
使用boost::signals2::signals
。 该组件的特定聚合类型为Updateable
。 我希望Updateable
能够连接到UpdateComponent
的boost::signals2::signal
。 我应该注意, Updateable
的slot
是pure-virtual
。
下面是该代码的一个具体示例:
// This is the component that emits a boost::signals2::signal.
class UpdateComponent {
public:
UpdateComponent();
boost::signals2::signal<void (float)> onUpdate; // boost::signals2::signal
}
在UpdateComponent
的代码中的某个时刻,我执行onUpdate(myFloat)
; 我认为这类似于对其所有“侦听器”“激发” boost::signals2::signal
。
// The is the aggregate that should listen to UpdateComponent's boost::signals2::signal
class Updateable {
public:
Updateable();
protected:
virtual void onUpdate(float deltaTime) = 0; // This is the pure-virtual slot that listens to UpdateComponent.
UpdateComponent* m_updateComponent;
}
在Updateable
的构造函数中,我执行以下操作:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(&onUpdate);
}
我收到以下两个错误:
...Updateable.cpp:8: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&BalaurEngine::Traits::Updateable::onUpdate' [-fpermissive]
/usr/include/boost/function/function_template.hpp:225: error: no match for call to '(boost::_mfi::mf1<void, BalaurEngine::Traits::Updateable, float>) (float&)'
我应该提到我将Qt与boost结合使用。 但是,我已经将CONFIG += no_keywords
添加到了我的.pro
文件中,因此,如boost网站上所述,这两者应该可以平滑地协同工作。 我不使用Qt的signals
和slots
(效果很好)的原因是:我不希望Updateable
成为QObject
。
如果有人可以帮助我弄清楚为什么我会出错,将不胜感激!
您要传递的connect
插槽必须是函子。 要连接到成员函数,可以使用boost::bind
或C ++ 11 lambda
。 例如使用lambda:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(
[=](float deltaTime){ onUpdate(deltaTime); });
}
或使用bind
:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(
boost::bind(&Updateable::onUpdate, this, _1));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.