C++中的智能指针实际上是代理模式与RAII的结合。

自定义unique_ptr,主要是release()和reset()。代码如下。

#include <iostream>
using namespace std; template<typename T>
class my_unique_ptr {
public:
my_unique_ptr(T *p = 0): pointee(p) {};
~my_unique_ptr() {
delete pointee;
}
T &operator * () const {
return *pointee;
}
T *operator -> () const {
return pointee;
}
T *get() {
return pointee;
}
T *release() {
T *oldPointee = pointee;
pointee = 0;
return oldPointee;
}
void reset(T *p = 0) {
if(pointee != p) {
delete pointee;
pointee = p;
}
}
private:
my_unique_ptr(const my_unique_ptr<T> &rhs);
my_unique_ptr<T> &operator = (const my_unique_ptr<T> &rhs); T *pointee;
}; struct C {
~C() {
cout << "destructor" << endl;
};
void print() {
cout << "print" << endl;
};
}; int main() {
my_unique_ptr<C> u1;
cout << u1.get() << endl; //0 my_unique_ptr<C> u2(new C());
u2->print(); //print my_unique_ptr<C> u3(u2.release());
cout << u2.get() << endl; //0
u3->print(); //print my_unique_ptr<C> u4;
u4.reset(u3.release());
cout << u3.get() << endl; //0
u4->print(); //print return 0; //destructor
}

自定义shared_ptr,主要是引用计数,代码如下。

#include <iostream>
using namespace std; template<typename T>
class my_shared_ptr {
public:
my_shared_ptr(T *p = 0): pointee(p), count(new size_t(0)) {
if(p) ++*count;
};
my_shared_ptr(const my_shared_ptr<T> &rhs): pointee(rhs.pointee), count(rhs.count) {
++*count;
}
my_shared_ptr<T> &operator = (const my_shared_ptr<T> &rhs) {
if(&rhs != this) {
decrease_count(); pointee = rhs.pointee;
count = rhs.count;
++*count;
}
return *this;
}
~my_shared_ptr() {
decrease_count();
}
size_t use_count() {
return *count;
}
T &operator * () const {
return *pointee;
}
T *operator -> () const {
return pointee;
}
private:
void decrease_count() {
if(--*count == 0) {
delete pointee;
delete count;
}
} T *pointee;
size_t *count;
}; struct C {
~C() {
cout << "destructor" << endl;
};
void print() {
cout << "print" << endl;
};
}; int main() {
my_shared_ptr<C> p1;
cout << "p1: " << p1.use_count() << endl; //p1: 0 my_shared_ptr<C> p2(new C());
cout << "p2: " << p2.use_count() << endl; //p2: 1 my_shared_ptr<C> p3(p2);
cout << "p3: " << p3.use_count() << endl; //p3: 2 my_shared_ptr<C> p4 = p3;
cout << "p4: " << p4.use_count() << endl; //p4: 3 {
my_shared_ptr<C> p5(p4);
cout << "p5: " << p5.use_count() << endl; //p5: 4
} cout << "p2: " << p2.use_count() << endl; //p2: 3
cout << "p3: " << p3.use_count() << endl; //p3: 3
cout << "p4: " << p4.use_count() << endl; //p4: 3 (*p4).print(); //print
p4->print(); //print return 0; //destructor
}

C++实现具有基本功能的智能指针的更多相关文章

  1. Binder学习笔记(十一)—— 智能指针

    轻量级指针 Binder的学习历程爬到驱动的半山腰明显感觉越来越陡峭,停下业务层的学习,补补基础层知识吧,这首当其冲的就是智能指针了,智能指针的影子在Android源码中随处可见.打开framewor ...

  2. C++ 11 智能指针 lamda 以及一个 围棋程序

    lamda表达式使用 char* p = "Hello world"; ,nl = ; for_each(p,p+, [&](char i){ if(i=='e') ne+ ...

  3. Rust入坑指南:智能指针

    在了解了Rust中的所有权.所有权借用.生命周期这些概念后,相信各位坑友对Rust已经有了比较深刻的认识了,今天又是一个连环坑,我们一起来把智能指针刨出来,一探究竟. 智能指针是Rust中一种特殊的数 ...

  4. 02 | 自己动手,实现C++的智能指针

    第一步:针对单独类型的模板 为了完成智能指针首先第一步的想法. class shape_wrapper { public: explicit shape_wrapper( shape* ptr = n ...

  5. 基于C/S架构的3D对战网络游戏C++框架 _05搭建系统开发环境与Boost智能指针、内存池初步了解

    本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...

  6. c++ auto_ptr智能指针

    c++ auto_ptr智能指针 该类型在头文件memory中,在程序的开通通过 #include<memory> 导入,接下来讲解该智能指针的作用和使用. 使用方法: auto_ptr& ...

  7. C++智能指针简单剖析

    导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...

  8. [4] 智能指针boost::scoped_ptr

    [1]boost::scoped_ptr简介 boost::scoped_ptr属于boost库,定义在namespace boost中,包含头文件#include <boost/scoped_ ...

  9. [6] 智能指针boost::weak_ptr

    [1]boost::weak_ptr简介 boost::weak_ptr属于boost库,定义在namespace boost中,包含头文件 #include<boost/weak_ptr.hp ...

随机推荐

  1. java多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么?

    多线程有两种实现方法,分别是继承Thread类与实现Runnable接口 同步的实现方面有两种,分别是synchronized,wait与notify 先看一下java线程运行时各个阶段的运行状态 j ...

  2. git 以及 github 使用系列

    本人打算开始写一个有关git 使用,以及github 使用的系列文章了,一来可以自己总结,二来github用好了,可以存放自己的一些知识框架吧. 1.准备 : 有一个 github上的账号, wind ...

  3. js 增删改查

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  4. 数据降维技术(2)—奇异值分解(SVD)

    上一篇文章讲了PCA的数据原理,明白了PCA主要的思想及使用PCA做数据降维的步骤,本文我们详细探讨下另一种数据降维技术—奇异值分解(SVD). 在介绍奇异值分解前,先谈谈这个比较奇怪的名字:奇异值分 ...

  5. losbyday Linux下的强大工具之一akw(转),Shell必备

    简单使用:awk :对于文件中一行行的独处来执行操作 .awk -F :'{print $1,$4}'   :使用‘:’来分割这一行,把这一行的第一第四个域打印出来 .  详细介绍:AWK命令介绍 a ...

  6. (中等) HDU 4370 0 or 1,建模+Dijkstra。

    Description Given a n*n matrix C ij (1<=i,j<=n),We want to find a n*n matrix X ij (1<=i,j&l ...

  7. iOS开发之圆角指定

    如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架).而若要指定某几个角(小于4)为圆角而别的不变时 ...

  8. 【亲测】Python:解决方案:Python Version 2.7 required, which was not found in the registry

    好久不更新随笔了,今天因为数据可视化作业,想抓取一些人人网好友关系数据,于是开始尝试python,用到numpy模块,安装的时候提示: 'Python Version 2.7 required, wh ...

  9. IT技术网站汇总

    首先是比较著名的博客型的网站!一般来说在国外比较著名的博客基本上都是比较有影响力发起的或者建立的经常发布一些比较有思考力深入分析的文章! 博客媒体网站 1.www.ArsTechnica.com 2. ...

  10. 基于MDK编程STM32程序无法使用,硬件仿真在汇编窗口看到停留在“0x0800XXXX BEAB BKPT 0xAB //进入调试模式”

    为方便工作上做测试,移植FreeRTOS到STM32F103xx,先做简单的UART1 printf. 工程编译通过,运行逻辑也确认可行,可就是无法正常打印消息.对比了网上的移植工程也是一致,硬件仿真 ...