[英]How to connect a boost::signal2::signal to a pure virtual function?
I am using boost::signals2::signals
in a component, UpdateComponent
. 我在组件
UpdateComponent
使用boost::signals2::signals
。 A specific aggregate for this component is of type Updateable
. 该组件的特定聚合类型为
Updateable
。 I would like Updateable
to be able to connect to UpdateComponent
's boost::signals2::signal
. 我希望
Updateable
能够连接到UpdateComponent
的boost::signals2::signal
。 I should note that the Updateable
's slot
is pure-virtual
. 我应该注意,
Updateable
的slot
是pure-virtual
。
Below is a concrete example of the code: 下面是该代码的一个具体示例:
// This is the component that emits a boost::signals2::signal.
class UpdateComponent {
public:
UpdateComponent();
boost::signals2::signal<void (float)> onUpdate; // boost::signals2::signal
}
At some point in UpdateComponent
's code, I perform onUpdate(myFloat)
; 在
UpdateComponent
的代码中的某个时刻,我执行onUpdate(myFloat)
; I believe this is akin to "firing" the boost::signals2::signal
to all of its "listeners". 我认为这类似于对其所有“侦听器”“激发”
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;
}
In Updateable
's constructor, I do the following: 在
Updateable
的构造函数中,我执行以下操作:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(&onUpdate);
}
I receive the following two errors: 我收到以下两个错误:
...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&)'
I should mention I am using Qt in conjunction with boost. 我应该提到我将Qt与boost结合使用。 However, I have added
CONFIG += no_keywords
to my .pro
file, so the two should be work together smoothly, as outlined on the boost website. 但是,我已经将
CONFIG += no_keywords
添加到了我的.pro
文件中,因此,如boost网站上所述,这两者应该可以平滑地协同工作。 The reason I don't use Qt's signals
and slots
(which works very well) is: I do not want Updateable
to be a QObject
. 我不使用Qt的
signals
和slots
(效果很好)的原因是:我不希望Updateable
成为QObject
。
If someone could help me figure out why I am getting an error, it would be greatly appreciated! 如果有人可以帮助我弄清楚为什么我会出错,将不胜感激!
The slot you are passing to connect
must be a functor. 您要传递的
connect
插槽必须是函子。 To connect to a member function, you can use either boost::bind
or C++11 lambda
. 要连接到成员函数,可以使用
boost::bind
或C ++ 11 lambda
。 For example using lambda: 例如使用lambda:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(
[=](float deltaTime){ onUpdate(deltaTime); });
}
or using bind
: 或使用
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.