将函数连接到 boost::signal 运行,但不调用该函数
我的代码中有一个类 Yarl
,其中包含一个成员函数 refresh
,我想将其绑定到两个 boost::signal
。其中一个信号是定义如下的类 EventHandler
的成员:
class EventHandler {
public:
boost::signal<void()> sigRefresh;
};
另一个是另一个文件中的自由浮动信号,声明如下:
namespace utility {
static boost::signal<void()> signal_refresh;
}
在 Yarl
的成员函数中,我将 refresh
连接到这样的信号:
events::EventHandler eventHandler;
eventHandler.sigRefresh.connect(boost::bind(&Yarl::refresh, this));
utility::signal_refresh.connect(boost::bind(&Yarl::refresh, this));
然后我像这样调用两个信号:
sigRefresh();
signal_refresh();
此代码编译并运行,并且 sigRefresh
完全按照预期工作。但是,当我调用 signal_refresh
时,没有任何反应。据我所知,refresh
实际上从未连接到signal_refresh
。有人看到我做错了什么吗?
I have a class Yarl
in my code with a member function refresh
that I want to bind to two boost::signal
s. One of these signals is a member of a class EventHandler
defined like this:
class EventHandler {
public:
boost::signal<void()> sigRefresh;
};
The other is a free floating signal in another file declared like this:
namespace utility {
static boost::signal<void()> signal_refresh;
}
in a member function of Yarl
, I connect refresh
to the signals like this:
events::EventHandler eventHandler;
eventHandler.sigRefresh.connect(boost::bind(&Yarl::refresh, this));
utility::signal_refresh.connect(boost::bind(&Yarl::refresh, this));
and later on I call both signals like this:
sigRefresh();
signal_refresh();
This code compiles and runs, and sigRefresh
works exactly as expected. However, nothing happens when I call signal_refresh
. As far as I can tell, refresh
never actually got connected to signal_refresh
. Anyone see what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜测您正在多重定义 signal_refresh。声明之前的 static 关键字向我表明代码片段位于头文件中,您必须将 static 放在那里才能使其编译而不会出现重新定义的符号错误。如果您这样做了,那么每个源文件(包括标头)都将获得 signal_refresh 的唯一副本,因此您正在调用的实例可能不是您连接到的实例。
我可能完全偏离了主题,但这是可能的。
I'm taking a guess that you are multiply defining signal_refresh. The static keyword before it's declaration suggests to me the code snippet is in a header file and you had to put the static there to get it to compile without redefined symbol errors. If you have done this then every source file including the header will get a unique copy of signal_refresh and thus the instance you are calling may not be the instance you connected it to.
I may be totally off mark here but it is possible.