借助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
- C++字符串操作小结
忽略大小写比较大小 库函数strcasecmp和_stricmp: 这两个函数都不属于C++标准库,strcasecmp由POSIX引入,windows平台则定义了功能等价的_stricmp.用法和C ...
- python 登陆三次错误退出
登陆出现三次错误,退出程序 1 #登陆 2 def Login(): 3 a = input() 4 if a == 'Kate': 5 b = input() 6 if b == '666666': ...
- fedora30平台安装docker 19.03
一,下载docker 1,说明:docker的打包对于fedora的支持很及时, 所以在fedora 30/31上都可以直接使用官方的rpm包 下载地址: https://download.docke ...
- centos8平台:redis6配置启用io多线程(redis6.0.1)
一,linux平台上redis6的安装 请参见这一篇: https://www.cnblogs.com/architectforest/p/12830056.html 说明:刘宏缔的架构森林是一个专注 ...
- lumen中间件 Middleware
app/http 下新建 TestMiddleware.php <?php namespace App\Http\Middleware; use Closure; class TestMiddl ...
- centos8平台使用blkid查看分区信息
一,blkid的用途 blkid 命令是一个命令行工具,它可以显示关于可用块设备的信息 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/archite ...
- selenium切换iframe
from selenium import webdriver br = webdriver.Chrome() br.get("tps://study.163.com/") ifra ...
- MVC联想查询绑定下拉框
前言 在做搜索时,输入些内容时需要弹出下拉框给用户进行选择,极大的方便了用户,会给用户带来不一样的体验 Controller public ActionResult SSAC(string UserN ...
- SpringCloud 与 SpringBoot版本问题
如果SpringBoot版本与SpringCloud版本不一致,SpringBoot应用启动会报错: 解决方案: 版本对应关系可以在 https://start.spring.io/info 上查看: ...