提高async_wait并添加"this"束缚中 [英] boost async_wait and adding "this" in a bind

查看:100
本文介绍了提高async_wait并添加"this"束缚中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个在类内部使用boost异步计时器的示例中,作者在m_timer.async_wait方法内部的绑定函数中添加了"this"指针.

这很奇怪,因为处理程序是不带任何参数的公共方法(message(void)),那么为什么要使用boost :: bind尤其是指针"this"呢?

class handler
{
public:
  handler(boost::asio::io_service& io)
    : m_timer(io, boost::posix_time::seconds(1)),
      m_count(0)
  {
    m_timer.async_wait(boost::bind(&handler::message, this));
  }

  ~handler()
  {
    std::cout << "The last count : " << m_count << "\n";
  }

  void message()
  {
    if (m_count < 5)
    {
      std::cout << m_count << "\n";
      ++m_count;

      m_timer.expires_at(m_timer.expires_at() + boost::posix_time::seconds(1));
      m_timer.async_wait(boost::bind(&handler::message, this));
    }
  }

private:
  boost::asio::deadline_timer m_timer;
  int m_count;
};

int main()
{
  boost::asio::io_service io;
  handler h(io);
  io.run();

  return 0;
}

解决方案

void handler::message()是非静态成员函数,因此必须在 handler 类型的对象上调用(或的派生类型).

这进一步意味着,在尝试将其作为回调传递给其他函数时,必须说要在哪个对象上调用此成员函数.

m_timer.async_wait(boost::bind(&handler::message,    this));
//                             ^- call this function ^- on this object

通过将this传递给boost::bind,我们表示我们希望在当前对象(即this)上调用地址为&handler::message的成员函数./p>


注意:整个表达式等效于告诉m_timer.async_wait调用this->handler::message()(或简称为this->message()).

In this example of using a boost asynchronous timer inside a class, the author added "this" pointer to the bind function inside m_timer.async_wait method.

That's strange because the handler is a public method (message(void)) that takes no argument, so why the hell using boost::bind and especially the pointer "this" ?

class handler
{
public:
  handler(boost::asio::io_service& io)
    : m_timer(io, boost::posix_time::seconds(1)),
      m_count(0)
  {
    m_timer.async_wait(boost::bind(&handler::message, this));
  }

  ~handler()
  {
    std::cout << "The last count : " << m_count << "\n";
  }

  void message()
  {
    if (m_count < 5)
    {
      std::cout << m_count << "\n";
      ++m_count;

      m_timer.expires_at(m_timer.expires_at() + boost::posix_time::seconds(1));
      m_timer.async_wait(boost::bind(&handler::message, this));
    }
  }

private:
  boost::asio::deadline_timer m_timer;
  int m_count;
};

int main()
{
  boost::asio::io_service io;
  handler h(io);
  io.run();

  return 0;
}

解决方案

void handler::message() is a non-static member-function, as such it must be invoked on an object of type handler (or a derived type thereof).

This further means that we must say on which object this member-function is to be invoked when trying to pass it as a callback to some other function.

m_timer.async_wait(boost::bind(&handler::message,    this));
//                             ^- call this function ^- on this object

By passing this to boost::bind as you have shown, we express that we would like to invoke the member-function, who's address is &handler::message, on the current object (ie. this).


Note: The whole expression is equivalent telling m_timer.async_wait to call this->handler::message() (or this->message() for short).

这篇关于提高async_wait并添加"this"束缚中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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