C ++成员函数指针,boost :: signals [英] C++ member function pointer, boost::signals

查看:136
本文介绍了C ++成员函数指针,boost :: signals的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况,(用代码更好)

I have the following situation, (better in code)

    class Foo
    {
private:
       typedef boost::signal<void ()> Signal;
       Signal signal;

public:
       void Register_SignalFunction(const Signal::slot_type& slot);
       void Unregister_SignalFunction(const Signal::slog_type& slot);
    }

    class OtherFoo
    {
       Foo foo;
public:
       OtherFoo()
       {
            foo.Register_SignalFunction(&OnSignal)    //I know I can't do this that is precisely my question.
       }

       void OnSignal();  //what to do when signal fires
     }  

所以问题是,我如何将成员函数"指针传递给Register方法.还可以吗我想要/需要的是某种代表注册系统,因此,如果谁能指出我正确的方向,我将不胜感激.提前感谢.

So the question is, how i pass a 'member-function' pointer to the Register method. Also, is this ok? What I want/need, is some sort of delegate registration system, so if anynone could point my in the right direction I'll appreciate it. Thanx in advance.

推荐答案

您通常会使用Boost绑定:

You would typically use boost bind:

foo.Register_SignalFunction(boost :: bind(& OtherFoo :: OnSignal,this));

foo.Register_SignalFunction(boost::bind(&OtherFoo::OnSignal, this));

这是怎么回事? :-)

What's going on here? :-)

信号的连接方法需要一个函子.那是一个实现()运算符的对象. bind获取函数指针(指向自由函数或成员函数),并返回具有正确签名的函子.

The connect method of the signal takes a functor. That is an object that implements the () operator. bind takes function pointers (either to free functions or member functions) and returns a functor with the right signature.

也请参见此处:

使用Boost :: Signals进行C ++事件的完整示例

在这里:

如何boost :: function和boost :: bind工作

要断开信号连接,请将返回值从connect存储到:

To disconnect a signal store the return value from connect into a:

boost::signals::connection

然后在其上调用断开连接方法.

And then call the disconnect method on that.

这篇关于C ++成员函数指针,boost :: signals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆