Ale*_*lex 2 c++ architecture qt boost signals
我boost::signals2::signals在一个组件中使用,UpdateComponent.此组件的特定聚合类型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
}
Run Code Online (Sandbox Code Playgroud)
在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;
}
Run Code Online (Sandbox Code Playgroud)
在Updateable构造函数中,我执行以下操作:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(&onUpdate);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下两个错误:
...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); });
}
Run Code Online (Sandbox Code Playgroud)
或使用bind:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(
boost::bind(&Updateable::onUpdate, this, _1));
}
Run Code Online (Sandbox Code Playgroud)