Timer.4 - Using a member function as a handler
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的更多相关文章
- Thinkphp---------Call to a member function free_result() on a non-object
1.平时用框架用久了,直接执行原生的sql反而做起来反应迟钝了.今天遇到一个问题,就是直接执行一个添加的sql语句,然后我用了TP框架的M()->query();方法.运行以后,会报Call t ...
- :( Call to a member function Table() on a non-object 错误位置
:( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form
- 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. ...
- 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 ...
- 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 ...
- Function语义学之member function
之前我们讲过编译器会对 nonmember functions 进行怎样的扩充和该写,今天我们来讲一下 member functions 函数调用方式 一.Nonstatic Member Funct ...
- C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化
模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...
- 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 ...
- 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 ...
随机推荐
- AIX 7.1 安装 Python2.7.5
一.下载必要的安装包 1. gdbm-1.8.3-5.aix5.2.ppc.rpm 下载地址:ftp://ftp.software.ibm.com/aix/freeSoftware/aixtoolb ...
- jQuery制作焦点图(轮播图)
焦点图(轮播图) 案例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- Asp.Net EF Code First 简单入门
今天在上班期间花了点时间学习了一下微软的EntityFramework Code First技术,这篇文章只是简单的入门,不多废话,下面直入主题. 一.首先添加一个解决方案,接着添加一个web网站,D ...
- 使用区域组织 ASP.NET MVC 应用程序
MVC 模式可将应用程序的模型(数据)逻辑与其呈现逻辑和业务逻辑分离. 在 ASP.NET MVC 中,这种逻辑分离还在项目结构中以物理方式实现,在该项目结构中,控制器和视图保存在使用命名约定定义关系 ...
- Android中多表的SQLite数据库(译)
原文: Android SQLite Database with Multiple Tables 在上一篇教程Android SQLite Database Tutorial中,解释了如何在你的And ...
- sql两个表联合更新
update TableA set id=TableB.id from TableA,TableB where TableA.name=TableB.name
- gitolite随记
1.git clone源码 git clone git://github.com/sitaramc/gitolite 2.安装 gitolite/install -ln 3.建立git仓库 gitol ...
- c#中serialPort1_DataReceived串口接收事件处理
1.缓冲区不定字节读取(波特率很高也没问题) //Thread.sleep(1000);//处理事件这块可以加上延时确保不定数的数据可以全部收到缓冲后,才去读缓冲内容--单位:毫秒 byte[] da ...
- Bootstrap学习笔记(未整理)
强调class 这些class通过颜色来表示强调.也可以应用于链接,当鼠标盘旋于链接上时,其颜色会变深,就像默认的链接样式. <p class="text-muted"> ...
- NAS4Free 安装配置(六)配置transmission实现BT(PT)下载
配置transmission transmission是一个跨平台的BT客户端 首先我们建立一个存放transmission配置文件的目录 可以通过SSH,也可以通过网页来完成 注意:最好是通过SSH ...