C++实现具有基本功能的智能指针
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++实现具有基本功能的智能指针的更多相关文章
- Binder学习笔记(十一)—— 智能指针
轻量级指针 Binder的学习历程爬到驱动的半山腰明显感觉越来越陡峭,停下业务层的学习,补补基础层知识吧,这首当其冲的就是智能指针了,智能指针的影子在Android源码中随处可见.打开framewor ...
- C++ 11 智能指针 lamda 以及一个 围棋程序
lamda表达式使用 char* p = "Hello world"; ,nl = ; for_each(p,p+, [&](char i){ if(i=='e') ne+ ...
- Rust入坑指南:智能指针
在了解了Rust中的所有权.所有权借用.生命周期这些概念后,相信各位坑友对Rust已经有了比较深刻的认识了,今天又是一个连环坑,我们一起来把智能指针刨出来,一探究竟. 智能指针是Rust中一种特殊的数 ...
- 02 | 自己动手,实现C++的智能指针
第一步:针对单独类型的模板 为了完成智能指针首先第一步的想法. class shape_wrapper { public: explicit shape_wrapper( shape* ptr = n ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- c++ auto_ptr智能指针
c++ auto_ptr智能指针 该类型在头文件memory中,在程序的开通通过 #include<memory> 导入,接下来讲解该智能指针的作用和使用. 使用方法: auto_ptr& ...
- C++智能指针简单剖析
导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...
- [4] 智能指针boost::scoped_ptr
[1]boost::scoped_ptr简介 boost::scoped_ptr属于boost库,定义在namespace boost中,包含头文件#include <boost/scoped_ ...
- [6] 智能指针boost::weak_ptr
[1]boost::weak_ptr简介 boost::weak_ptr属于boost库,定义在namespace boost中,包含头文件 #include<boost/weak_ptr.hp ...
随机推荐
- MIME小知识
http://www.alixixi.com/program/a/2008020514775.shtml 用户可以通过使用MIME以设置服务器传送多媒体如声音和动画信息,这一切可能通过CGI脚本来进行 ...
- fuel健康检查Heat失败的原因
service openstack-heat-engine restart chkconfig --level 2345 openstack-heat-engine on
- Protocol buffers--python 实践(一) 简介以及安装与使用
由于最近对grpc产生了浓厚的兴趣,但是那是一整套东西,看了一下用到的东西不少,所以抽丝剥茧先写写这几天调研的protocol buffer -python的收获. 简介: 以下引用自官方首页文档: ...
- ssh密钥登录及远程执行命令
以192.168.1.104作为客户机 以192.168.1.103作为服务器 使用密钥登录 创建密钥对 在SSH客户机创建用户秘钥对 ssh-keygen -t rsa 之后全回车即可 将会在~/. ...
- CSS Font-family常用设置
font-family: "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Lantinghe ...
- 多线程编程--- __thread关键字
__thread是GCC内置的线程局部存储设施,存取效率可以和全局变量相比.__thread变量每一个线程有一份独立实体,各个线程的值互不干扰.可以用来修饰那些带有全局性且值可能变,但是又不值得用全局 ...
- 模块之字节编译的.pyc文件---from.import语句
字节编译的.pyc文件输入一个模块相对来说是一个比较费时的事情,所以Python做了一些技巧,以便使输入模块更加快一些.一种方法是创建 字节编译的文件 ,这些文件以.pyc作为扩展名.字节编译的文件与 ...
- EM 期望最大化算法
(EM算法)The EM Algorithm EM是我一直想深入学习的算法之一,第一次听说是在NLP课中的HMM那一节,为了解决HMM的参数估计问题,使用了EM算法.在之后的MT中的词对齐中也用到了. ...
- MySQL的mysql_insert_id和LAST_INSERT_ID
摘要:mysql_insert_id和LAST_INSERT_ID二者作用一样,均是返回最后插入值的ID 值 1 mysql_insert_id 一.PHP获取MYSQL新插入数据的ID mysql ...
- 文本去重-----awk或者uniq
对于awk '!a[$3]++',需要了解3个知识点 1.awk数组知识,不说了 2.awk的基本命令格式 awk 'pattern{action}' 省略action时,默认action是{ ...