In this tutorial we will see how to use a class member function as a callback handler. The program should execute identically to the tutorial program from tutorial Timer.3.

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

Instead of defining a free function print as the callback handler, as we did in the earlier tutorial programs, we now define a class called printer.

class printer
{
public:

The constructor of this class will take a reference to the io_service object and use it when initialising the timer_ member. The counter used to shut down the program is now also a member of the class.

  printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds(1)),
count_(0)
{

The boost::bind() function works just as well with class member functions as with free functions. Since all non-static class member functions have an implicit this parameter, we need to bind this to the function. As in tutorial Timer.3, boost::bind() converts our callback handler (now a member function) into a function object that can be invoked as though it has the signature void(const boost::system::error_code&).

You will note that the boost::asio::placeholders::error placeholder is not specified here, as the print member function does not accept an error object as a parameter.

    timer_.async_wait(boost::bind(&printer::print, this));
}

In the class destructor we will print out the final value of the counter.

  ~printer()
{
std::cout << "Final count is " << count_ << "\n";
}

The print member function is very similar to the print function from tutorial Timer.3, except that it now operates on the class data members instead of having the timer and counter passed in as parameters.

  void print()
{
if (count_ < 5)
{
std::cout << count_ << "\n";
++count_; timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this));
}
} private:
boost::asio::deadline_timer timer_;
int count_;
};

The main function is much simpler than before, as it now declares a local printer object before running the io_service as normal.

int main()
{
boost::asio::io_service io;
printer p(io);
io.run(); return 0;
}

See the full source listing

Return to the tutorial index

Previous: Timer.3 - Binding arguments to a handler

Next: Timer.5 - Synchronising handlers in multithreaded programs

Timer.4 - Using a member function as a handler的更多相关文章

  1. Thinkphp---------Call to a member function free_result() on a non-object

    1.平时用框架用久了,直接执行原生的sql反而做起来反应迟钝了.今天遇到一个问题,就是直接执行一个添加的sql语句,然后我用了TP框架的M()->query();方法.运行以后,会报Call t ...

  2. :( Call to a member function Table() on a non-object 错误位置

    :( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form

  3. Fatal error: Call to a member function bind_param() on a non-object in

    今天在练习 mysql是出现错误:   Fatal error: Call to a member function bind_param() on a non-object in 解决步骤: 1. ...

  4. ECmall错误:Call to a member function get_users_count() on a non-object

    问题描述: 在后台添加了一个app报错:Call to a member function get_users_count()Fatal error: Call to a member functio ...

  5. magento后台 Fatal error: Call to a member function getId() on a non-object in错误

    后台分类管理出现错误 Fatal error: Call to a member function getId() on a non-object in 在数据库中运行以下sql语句 INSERT I ...

  6. Function语义学之member function

    之前我们讲过编译器会对 nonmember functions 进行怎样的扩充和该写,今天我们来讲一下 member functions 函数调用方式 一.Nonstatic Member Funct ...

  7. C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化

    模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...

  8. About The Order of The Declarations And Definition When Making a Member Function a Friend.关于使类成员成为另一个类友元函数的声明顺序和定义。

    If only member function clear of WindowMgr is a friend of Screen, there are some points need to note ...

  9. About Why Inline Member Function Should Defined in The Header File

    About why inline member function should defined in the header file. It is legal to specify inline on ...

随机推荐

  1. Linux 运维笔记

    #配置静态地址网卡DEVICE="eth0"BOOTPROTO=staticHWADDR="00:0C:29:DC:EA:F7"NM_CONTROLLED=&q ...

  2. 虚拟现实,增强现实,VR,AR

    现在的热点不止VR,还有AR和披着MR.HR.CR外衣的各种高级AR们,所以比较着一起说.以下知乎上一网友观点,放几条结论:1.近期(未来两三年)看,VR能火,AR尚待成熟: 2.VR设备中,插片式是 ...

  3. javascriptt切换组件MyTab.js封装

    之前做的大多数是jquery的插件,就优雅性来说,我觉得还是原生的代码,写起来更舒服一点,虽然麻烦很多. 之前写了一个利用完美运动框架的轮播效果,因为使用的是原生的代码,因为不懂原生对象封装的原因一直 ...

  4. Asp.Net HttpApplication请求管道与Session(二)

    Asp.Net 回话的创建与结束 LogHelper.LogHelper _log = new LogHelper.LogHelper(); /// <summary> /// 程序开始- ...

  5. 实现VS2010整合NUnit进行单元测试

    1.下载安装NUnit(最新win版本为NUnit.3.2.1.msi) http://www.nunit.org/index.php?p=download 2.下载并安装VS的Visual Nuni ...

  6. mercurial(Hg) Server 搭建 过程记录

    mercurial(Hg) Server 搭建 过程记录 1.  环境说明 只是测试搭建,环境为本机开发环境:win 8.1 + IIS8.5 软件准备: 2.  软件安装 先安装Python2.7, ...

  7. 二维码生成 - QrCodeNet

    下载QrCodeNet /// <summary> /// 生成QR码 /// </summary> /// <param name="output_path& ...

  8. hdu Big Number 求一个数的位数

    Problem Description In many applications very large integers numbers are required. Some of these app ...

  9. 最短路径floy算法———模板

    #include<cstdio>int n,i[1000][1000];int main(){ scanf("%d",&n); for (int a=1;a&l ...

  10. SharpZipLib 压缩文档下载

    using ICSharpCode.SharpZipLib.Zip; Response.Clear(); Response.ClearContent(); Response.ClearHeaders( ...