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. 算法录 之 BFS和DFS

    说一下BFS和DFS,这是个比较重要的概念,是很多很多算法的基础. 不过在说这个之前需要先说一下图和树,当然这里的图不是自拍的图片了,树也不是能结苹果的树了.这里要说的是图论和数学里面的概念. 以上概 ...

  2. android4.0 的图库Gallery2代码分析(二)

    最近迫于生存压力,不得不给人兼职打工.故在博文中加了个求点击的链接.麻烦有时间的博友们帮我点击一下.没时间的不用勉强啊.不过请放心,我是做技术的,肯定链接没病毒,就是我打工的淘宝店铺.嘻嘻.http: ...

  3. 【HighCharts系列教程】八、Html标签属性——Labels

    一.labels属性说明 Labels属性允许在HighCharts图表的任意位置添加任意的html代码.可以实现许多自定义内容. 二.labels属性详解 参数 子参数 说明 默认值 items — ...

  4. Mac下安装包管理平台Homebrew(Mac 10.12)

    在终端上输入: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/maste ...

  5. 19、手把手教你Extjs5(十九)模块Grid的其他功能的设想

    经过对自定义模块和Grid的设计和编码,现在已经能对一个有配置信息的模块来生成界面并进行一些简单的CURD操作.由于这是一个全解释性的前台的架构,因此你想到的任何新主意都可以放到所有的模块中. 比如对 ...

  6. FZU 1061 矩阵连乘

    用栈来算一算就可以了. #include<iostream> #include<algorithm> #include<cstdio> #include<cs ...

  7. vs2012中的小技巧2

    vs代码前面出现.......,解决方法是:点击菜单编辑——高级——查看空白

  8. Java层与Jni层的数组传递(转)

    源:Java层与Jni层的数组传递 Android开发中,经常会在Java代码与Jni层之间传递数组(byte[]),一个典型的应用是Java层把需要发送给客户端的数据流传递到Jni层,由Jni层的S ...

  9. jQuery-ui datepicker的使用演示代码

    这两天使用jquery做一个web端展示的工具,遇到了不少问题也学到了不少知识.其中有一个就是在页面中显示日期选择器的功能,通过百度直接使用的是jquery datepicker 看到一篇使用说明很不 ...

  10. easyui 异步json tree跨域访问问题解决

    最近在用easyui中的异步tree时发现了跨域访问问题,我们都知道jquery ajax提供get请求的跨域访问.所以解决easyui tree跨域访问的问题便是将数据通过jquery ajax将数 ...