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;
}