【c++】简单模拟实现boost库下的shared-ptr

//简单模拟实现boost库下的shared_ptr

#include <iostream>
#include <string.h>
using namespace std;

class shared_ptr_Rep
{
	friend class shared_ptr;
public:
	shared_ptr_Rep(const char *str = " ") :count(0)
	{
		px = new char[strlen(str) + 1];
		strcpy(px, str);
	}
	~shared_ptr_Rep()
	{
		delete[]px;
	}
public:
	void increment()
	{
		count++;
	}
	void decrement()
	{
		if (--count == 0)
		{
			delete this;// 哪一个rep的count为0了释放当时的rep
		}
	}
	char* get()
	{
		return px;
	}
private:
	char *px;
	int count;
};
//////////////////////////////////////////////////////////////////////

class shared_ptr
{
public:
	shared_ptr(const char *str = " ") :rep(new shared_ptr_Rep(str))
	{
		rep->increment();
	}
	shared_ptr(const shared_ptr &s) :rep(s.rep)
	{
		rep->increment();
	}
	shared_ptr& operator=(const shared_ptr &s)
	{
		if (this != &s)
		{
			rep->decrement();    // 迭代器
			rep = s.rep;
			rep->increment();
		}
		return *this;
	}
	~shared_ptr()
	{
		rep->decrement();
	}
public:
	char& operator*()
	{
		return *(rep->px);
	}
	char* operator->()
	{
		return rep->px;
	}
private:
	shared_ptr_Rep *rep;
};

int main()
{
	char *p = new char('a');
	char *q = new char('b');
	shared_ptr s1(p);
	cout << *s1 << endl;
	shared_ptr s2 = s1;
	cout << *s2 << endl;
	shared_ptr s3(q);
	s3 = s2;
	cout << *s3 << endl;
	shared_ptr s4(q);
	cout << *s4 << endl;
	shared_ptr s5 = s4;
	cout << *s5 << endl;
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 08:36:35

【c++】简单模拟实现boost库下的shared-ptr的相关文章

【c++】模拟实现boost库下的scoped_array

//模拟实现boost库下的scoped_array #include <iostream> #include <assert.h> using namespace std; template <class T> class scoped_array { private: T * px; scoped_array(scoped_array const &); scoped_array& operator=(scoped_array const &

【c++】模拟实现boost库里的scoped_ptr

//模拟实现boost下的scoped_ptr #include <iostream> #include <assert.h> using namespace std; template <class T> class scoped_ptr { private: T * px; scoped_ptr(scoped_ptr const &); scoped_ptr& operator=(scoped_ptr const &); void opera

linux下编译安装boost库

转载:http://www.cnblogs.com/oloroso/p/4632848.html linux下编译安装boost库 linux下编译安装boost库 1.下载并解压boost 1.58 源代码 下载 解压 2.运行bootstrap.sh 3.使用b2进行构建 构建成功的提示 4.安装boost库到指定目录 5.测试一下 代码 编译运行 先看一下系统环境 Linux o-pc 3.19.0-22-generic #22-Ubuntu SMP Tue Jun 16 17:15:15

boost库在windows下的编译和使用

因为跨平台的原因,现在要使用到boost库,boost库非常大,现在处于摸索阶段. 首先来说boost库在window下的安装和使用. 一.下载 首先从boost官方主页http://www.boost.org/下载最新版boost安装包,或者使用Subversion获取最新版本,地址是:http://svn.boost.org/svn/boost/trunk.本人现在一般都用svn,这样可以很方便的进行update和build,而不是每次都下载新的安装包并rebuild. 二.安装 如果是使用

windows下安装boost库

工作中现在会接触boost,所以我计划两个月之内努力熟悉一下boost.今天在自己win10系统上尝试安装了boost库,下面把遇到的问题总结一下: 1. 下好1.61版本库,在boost目录下运行bootstrap.bat 时发现dos闪退且目录下并没有多出bjam.exe和b2.exe,以管理员权限运行也是没结果. 后来索性重启了下电脑,果然解决问题. 2. 接下来就是编译,编译命令的话上网搜下就有了,关键出现了CL不识别的问题,这个问题也好解决.在自己的boost目录下 shift+右键,

VS2008下直接安装使用Boost库1.46.1版本号

Boost库是一个可移植.提供源码的C++库,作为标准库的后备,是C++标准化进程的发动机之中的一个. Boost库由C++标准委员会库工作组成员发起,当中有些内容有望成为下一代C++标准库内容.在C++社区中影响甚大,是不折不扣的“准”标准库. Boost因为其对跨平台的强调,对标准C++的强调,与编写平台无关.大部分boost库功能的使用仅仅需包含对应头文件就可以,少数(如正則表達式库,文件系统库等)须要链接库.但Boost中也有非常多是实验性质的东西,在实际的开发中有用须要慎重.boost

在VS2013下配置BOOST库

1.安装Boost库 (1).首先打开Boost的官网(http://www.boost.org/),找到下载位置,如下图中红框所示,此时最新的版本是1.64.0: (2).点击进入下载页面,选择你需要的文件下载,这里我选择windows下的zip文件: (3).下载好后,解压,得到文件目录如下图,找到其中的bootstrap.bat文件: (4).双击运行bootstrap.bat后,让其自动运行完成后,会发现当前文件夹中增加了几个文件,找到其中的bjam.exe,如下图所示 (5).双击运行

Linux/ubuntu下的boost库安装

我一直都没有写博客的习惯,最近正好在研究linux下的开发(目前也只是粗粗的研究),且用到了boost库,就乘此机会写点什么,最起码记录一下我在安装boost的一些步骤,主要给和我一样的linux开发新手们提供点借鉴(当然如果看到这篇文档的话 ),作者(jwybobo2007). 1.下载boost库 这个我就不说啥了,去官网看一下就能下到:www.boost.org ,现在的版本更新到了1.44 2.执行:sudo apt-get install build-essential 不为别的,就是

linux 下安装boost 库

a. 在 www.boost.org 下载 boost_1_59_0 源码包,解压缩. b. 进入目录后,运行 ./bootstrap.sh ,会生成一个 bjam 的可执行程序. c. 运行 ./bjam 进行编译.等待... d. 编译成功后,在 bjam 同级文件夹下,会有 stage.bin.v2 两个目录.bin.v2 中存的是编译时生成的目标文件,stage 中放的是 boost 的库文件. e. 新建目录  /usr/share/boost_1_59_0/ .把 stage.boo