借助boost bind/function来实现基于对象编程。
boost bind/function库的使用:
替换了stl中mem_fun,bind1st,bin2nd等函数。用户注册回调函数需要利用boost/bind转化成库中boost/function格式的函数。然后调用库的时候就可以回调用户注册的处理函数了。bind也可以将成员函数转换成boost/function指定的函数格式。
#include<iostream>
#include<boost/function.hpp>
#include<boost/bind.hpp> using namespace std;
class Foo
{
public:
void memberFunc(double d,int i,int j)
{
cout << d << endl;
cout << i << endl;
cout << j << endl;
}
}; int main()
{
Foo foo;
//将一种函数类型的接口转换为另一种函数类型的接口。
//将这个成员函数&Foo::memberFuncvoid适配成void f(int)这样的接口。
//成员函数有4个参数this d i j,适配成的函数接口有一个参数。
boost::function<void(int)> fp = boost::bind(&Foo::memberFunc,&foo/*this指针参数*/,0.5,_1,10);
fp(100);
boost::function<void(int,int)> fp = boost::bind(&Foo::memberFunc, &foo, 0.5, _1, _2);
fp(100,200);
return 0;
}
利用boost bind/function实现基于对象的Thread类:
#ifndef _THREAD_H_
#define _THREAD_H_ #include <pthread.h> class
{
public:
typedef boost::function(void()) ThreadFunc;
explicit Thread(const ThreadFunc& func);//阻止隐式转换构造。
void Start();
void Join();
void SetAutoDelete(bool autoDelete);
private:
static void* ThreadRoutine(void* arg);
void Run();
ThreadFunc func_;
pthread_t threadId_;
bool autoDelete_;
}; #endif
//基于对象的方法编写Thread类。
/*
Start()、Join()、ThreadRoutine(void* arg)线程入口函数调用Run()、Run()执行体函数。
typedef boost::function<void()> ThreadFunc;
面向对象的方法中:线程的执行体函数由派生类Run()方法来覆盖积累中的Run()来实现。
基于对象时:线程的执行体函数由构造函数传入ThreadFunc方法。Run()调用这个ThreadFunc();
*/
#include"Thread.h"
#include<iostream>
using namespace std; Thread::Thread(const ThreadFunc& func):autoDelete_(false),func_(func)
{
cout << "Thread..." << endl;
} Thread()::~Thread()
{
cout << "~Thread..." << endl;
} void Thread::Start()
{
pthread_create(&threadId_,NULL,ThreadRoutine,this);
} void Thread::Join()
{
pthread_join(threadId_,NULL);
} void ThreadRoutine(void* arg)
{
Thread* thread = static_cast<Thread*>(arg);
thread->Run();
if (thread->autoDelete_)
delete thread;
return NULL;
} void Thread::SetAutoDelete(bool autoDelete)
{
autoDelete_ = autoDelete;
} void Thread::Run()
{
func_();
}
void ThreadFunc(int count)
{
cout << "threadfunc" << endl;
}
int main(void)
{
Thread t1(boost::bind(ThreadFunc,3));
t1.Start();
t1.Join();
return 0;
}
借助boost bind/function来实现基于对象编程。的更多相关文章
- 《Essential C++》读书笔记 之 基于对象编程风格
<Essential C++>读书笔记 之 基于对象编程风格 2014-07-13 4.1 如何实现一个class 4.2 什么是Constructors(构造函数)和Destructor ...
- boost bind function用法说明
目录(?)[+] 1 bind/function 引 (1)头文件 bind函数#include <boost/bind.hpp> function使用头文件#include <bo ...
- JavaScript基于对象编程
js面向对象特征介绍 javascript是一种面向(基于)对象的动态脚本语言,是一种基于对象(Object)和事件驱动(EventDirven)并具有安全性能的脚本语言.它具有面向对象语言所特有的各 ...
- JavaScript学习总结(九)——Javascript面向(基于)对象编程
一.澄清概念 1.JS中"基于对象=面向对象" 2.JS中没有类(Class),但是它取了一个新的名字叫“原型对象”,因此"类=原型对象" 二.类(原型对象)和 ...
- js面向(基于)对象编程—类(原型对象)与对象
JS分三个部分: 1. ECMAScript标准--基础语法 2. DOM Document Object Model 文档对象模型 3. BOM Browser Object Moldel 浏览 ...
- JavaScript学习总结(5)——Javascript面向(基于)对象编程
一.澄清概念 1.JS中"基于对象=面向对象" 2.JS中没有类(Class),但是它取了一个新的名字叫"原型对象",因此"类=原型对象" ...
- JS面向(基于)对象编程--三大特征
抽象 在讲解面向对象编程的三大特征前,我们先了解什么叫抽象,在定义一个类时候,实际上就是把一类事物的共有的属性和行为提取出来,形成一个物理模型(模板).这种研究问题的方法称为抽象. 封装 什么是封装? ...
- JS面向(基于)对象编程--构造方法(函数)
构造函数(方法)介绍 什么是构造函数呢?在回答这个问题之前,我们来看一个需求:前面我们在创建人类的对象时,是先把一个对象创建好后,再给他的年龄和姓名属性赋值,如果现在我要求,在创建人类的对象时,就直接 ...
- boost::function和boost::bind 介绍
一. boost::function介绍 原文:http://www.cnblogs.com/sld666666/archive/2010/12/16/1907591.html 本片文章主要介绍boo ...
随机推荐
- 多测师讲解第一个月 _综合面试题_高级讲师肖sir
第一个月综合面试题 1. 冒烟测试是什么意思? 对主要的用例测试 2.你们公司的项目流程是什么? 3.你们公司的bug分几个级别? 4个 4.你对外键是怎么理解的? 你会使用外键吗?给一个表添加 ...
- day54 Pyhton 前端JS06
内容回顾 - ECMAScript5.0 基础语法 - var 声明变量 - 五种基本数据类型 - string - number NaN number 1 number - boolean - un ...
- scrapy LinkExtractors
class scrapy.linkextractors.LinkExtractor Link Extractors 的目的很简单: 提取链接。 每个LinkExtractor有唯一的公共方法是 ext ...
- Ubuntu18.04中安装virtualenv和virtualenvwrapper
1.安装virtualenv和virtualenvwrapper pip3 install virtualenv pip3 install virtualenvwrapper 2.创建目录用来存放虚拟 ...
- 12天搞定Python,基础语法(上)
不知你是否见过建楼房的过程,没有的话,找个时间去瞧一瞧,看一看.看过之后,你就会明白.建楼房,只有打好地基之后,才能在砌墙,建的楼层越高,打的地基就越深. 学编程也一样,要想得心应手的应用,得先打好地 ...
- thinkphp数组给js赋值 tp模板把数组赋值给js变量
var arr=transArr({$array|json_encode=true}); function transArr(obj) { var tem=[]; $.each(obj, functi ...
- Intellij IDEA创建Maven Web项目(带有webapp文件夹目录的项目)
每日技术点记录一下https://blog.csdn.net/mawei7510/article/details/83089268
- Java面试题集(二)list与Map相关知识(1.2)
前言: 在平常的写java程序中,常用除了8种常用数据类型,String对象外,还有集合类,例如ArrayList,HashMap等,这些最常用. 一.List接口 List接口为Collection ...
- k8s各组件启动时, -v参数指定的日志级别
k8s 相关组件启动时 -v参数指定的日志级别 --v=0 Generally useful for this to ALWAYS be visible to an operator. --v=1 A ...
- STM32入门系列-复位程序
已经对启动文有了大致了解,再来看看系统在复位过程中做了哪些工作.复位程序如下: 1 ; Reset handler 2 3 Reset_Handler PROC 4 5 EXPORT Reset_Ha ...