如何将 boost::signal2::signal 连接到纯虚函数
How to connect a boost::signal2::signal to a pure virtual function?
我在组件中使用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
}
在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));
}
相关文章:
- 是否有 Windows 用户空间函数来枚举连接的网络共享?
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- 您将如何连接"on the fly"文本+整数并将其传递给函数?
- 某些 boost::asio 异步函数是否将处理程序连接到操作,以便处理程序被触发一次?
- QObject::连接不起作用 - 使用函数语法找不到信号
- 在函数中连接两个字符,并在 C++ 中返回输出
- 用于连接向量的 Constexpr 函数
- deleteNode函数出现以中断连接列表
- QT - 连接 Qml 按钮 单击到 Cpp 构造函数
- Wt 连接函数,将参数传递给作为连接函数参数的函数
- Qt的新信号/时隙语法问题 - 连接到一个简单的函数
- C++ 将不同的类型连接成函数的字符串
- 如何编写模板函数来添加整数但连接字符串和字符类型?
- 在 for 循环中使用 lambda 函数连接信号插槽
- QObject::使用update()函数连接计时器
- 函数连接两个写入缓冲区开头的字符串
- 使用QObject::connect()时,类型签名是否重要,用于将函数连接到信号
- 如何将Qt动作与函数连接起来
- 如何从一个线程发出信号,并将信号与一个单独的函数连接起来
- QT 5 connect()函数连接textEditor和主窗口