C++智能指针实现
#include <iostream>
#include <string>
#define unsigned int size_t
using namespace std; // 未考虑线程安全
template<typename FriendClass, typename DataType>
class RefCount{
private:
DataType * IPtr;
size_t count; RefCount(DataType * p):IPtr(p),count(0){
print("构造函数");
} ~RefCount(){
print("析构函数");
delete IPtr;
IPtr = NULL;
} void increaseOne(){
this->count ++;
}
void decreaseOne(){
this->count --;
}
void print(string info){
cout << "RefCount:"<<info<<" Refcount:"<<count<<endl;
}
friend FriendClass;
}; template<typename DataType>
class SmartPtr{
private:
RefCount<SmartPtr,DataType> * ptr;
public:
SmartPtr( DataType * p):ptr(new RefCount<SmartPtr,DataType>(p)){
ptr->increaseOne();
print("构造函数");
}
SmartPtr(const SmartPtr& rhs):ptr(rhs.ptr){
ptr->increaseOne();
print("复制构造函数");
}
SmartPtr & operator =(const SmartPtr & rhs){
if(this == &rhs) return *this;
set(rhs.ptr);
print("赋值操作符函数");
return *this;
}
DataType & operator *(){
return * (ptr->IPtr);
}
DataType * operator ->(){
return ptr->IPtr;
}
DataType * getPtr(){
return ptr->IPtr;
}
DataType getValue(){
return *(ptr->IPtr);
}
void setPtr(DataType * newPtr){
if(newPtr == NULL) return;
this->set(new RefCount<SmartPtr,DataType>(newPtr));
}
void setValue(DataType newValue){
*(ptr->IPtr) = newValue;
}
~SmartPtr(){
print("析构函数");
reset();
}
void print(string info){
cout << "SmartPtr:"<<info<<" Value: "<<*(ptr->IPtr)<<" RefCount:"<<ptr->count<<endl;
} private:
void set(RefCount<SmartPtr,DataType> * newPtr){
reset();
ptr = newPtr;
ptr->increaseOne();
}
void reset(){
ptr->decreaseOne();
if(ptr->count == 0)
delete ptr;
ptr=NULL;
} }; int main()
{
//测试普通构造函数
{
SmartPtr<int> sp(new int(1));
/* 输出
RefCount:构造函数 Refcount:0
SmartPtr:构造函数 Value: 1 RefCount:1
SmartPtr:析构函数 Value: 1 RefCount:1
RefCount:析构函数 Refcount:0
*/
}
//测试复制构造函数
{
SmartPtr<int> sp(new int(2));
SmartPtr<int> sp1(sp);
/*输出
RefCount:构造函数 Refcount:0
SmartPtr:构造函数 Value: 2 RefCount:1
SmartPtr:复制构造函数 Value: 2 RefCount:2
SmartPtr:析构函数 Value: 2 RefCount:2
SmartPtr:析构函数 Value: 2 RefCount:1
RefCount:析构函数 Refcount:0
*/
} // 测试赋值操作符
{
SmartPtr<int> sp(new int(3)),sp1(new int(4));
sp = sp1;
/* 输出
RefCount:构造函数 Refcount:0
SmartPtr:构造函数 Value: 3 RefCount:1
RefCount:构造函数 Refcount:0
SmartPtr:构造函数 Value: 4 RefCount:1
RefCount:析构函数 Refcount:0
SmartPtr:赋值操作符函数 Value: 4 RefCount:2
SmartPtr:析构函数 Value: 4 RefCount:2
SmartPtr:析构函数 Value: 4 RefCount:1
RefCount:析构函数 Refcount:0
*/
}
//测试* ->操作符
{
SmartPtr<string> sp( new string("helloworld"));
cout << *sp <<endl;
sp->append(" 你好!");
cout << sp->c_str()<<endl;
*sp = "哈哈哈";
cout << sp.getValue()<<endl;
/*输出
RefCount:构造函数 Refcount:0
SmartPtr:构造函数 Value: helloworld RefCount:1
helloworld
helloworld 你好!
哈哈哈
SmartPtr:析构函数 Value: 哈哈哈 RefCount:1
RefCount:析构函数 Refcount:0
*/
}
//其他函数测试
{
SmartPtr<int> sp(new int(5));
cout << *(sp.getPtr())<<endl;
sp.setPtr(new int(6));
cout <<sp.getValue()<<endl;
sp.setValue(7);
cout << *sp<<endl;
/*输出
RefCount:构造函数 Refcount:0
SmartPtr:构造函数 Value: 5 RefCount:1
5
RefCount:构造函数 Refcount:0
RefCount:析构函数 Refcount:0
6
7
SmartPtr:析构函数 Value: 7 RefCount:1
RefCount:析构函数 Refcount:0
*/
}
// 内存泄露测试
{
while(true){ //在任务管理器中观察内存占用情况
int *p = new int(3);
int *q =new int(4); SmartPtr<int> sp1(p);
SmartPtr<int> sp3=sp1;
sp3.setPtr(q);
SmartPtr<int> sp4=sp3;
SmartPtr<int> sp5(new int(5));
sp1= sp5;
sp3=sp5;
sp4=sp5;
sp5.setPtr(new int(6));
int * pi = new int(8);
SmartPtr<int> *spa= new SmartPtr<int>(pi);
SmartPtr<int> *spb = new SmartPtr<int>(*spa);
SmartPtr<int> *spc = new SmartPtr<int>(*spb);
delete spa;
delete spb;
delete spc; } } }
参考文献 http://blog.csdn.net/ishallwin/article/details/4533145
C++智能指针实现的更多相关文章
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针
一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- C++ 引用计数技术及智能指针的简单实现
一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...
- C++11智能指针读书笔记;
智能指针是一个类对象,而非一个指针对象. 原始指针:通过new建立的*指针 智能指针:通过智能指针关键字(unique_ptr, shared_ptr ,weak_ptr)建立的指针 它的一种通用实现 ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
随机推荐
- GitHub 开启 Two-factor authentication,如何在命令行下更新和上传代码
最近在使用GitHub管理代码,在git命令行管理代码时候遇到一些问题. 如果开起了二次验证(Two-factor authentication两个要素认证),命令行会一直提示输入用户名和密码.查找了 ...
- hihoCoder #1165 : 益智游戏 (挑战赛11 B题)
题意:在一个序列中找到两个数a和b,使得a*b的因子个数最多,输出最多的因子个数. 思路:数据较多,处理会很慢.对序列中每个数字进行质数分解求因子个数,然后按照因子个数降序排列,对前50个因子最多的数 ...
- 51nod 1572 宝岛地图
题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 勇敢的水手们到达了一个小岛,在这个小岛上,曾经有海盗在这里埋下了一些宝藏.然而,我 ...
- ucos-ii核心算法分析(转)
μC/OS-Ⅱ是一种免费公开源代码.结构小巧.具有可剥夺实时内核的实时操作系统.其 内核提供任务调度与管理.时间管理.任务间同步与通信.内存管理和中断服务等功能.适合小型控制系统,具有执行效率高.占用 ...
- UVA1607 Gates 与非门电路 (二分)
题意:给你一个按发生时间的序列,表示与非门电路的输入,一开始全部输入是x,现在要改成尽量少的x,实现相同的功能. 题解:电路功能只有4中0,1,x,非x.那么如果一开始x改变了,输出结果不变,那么说明 ...
- Tarjan的学习笔记 求割边求割点
博主图论比较弱,搜了模版也不会用... 所以决心学习下tarjan算法. 割点和割边的概念不在赘述,tarjan能在线性时间复杂度内求出割边. 重要的概念:时间戟,就是一个全局变量clock记录访问结 ...
- 认识CoreData—初识CoreData
http://www.cocoachina.com/ios/20160729/17245.html 这段时间公司一直比较忙,和组里小伙伴一起把公司项目按照之前逻辑重写了一下.由于项目比较大,还要兼顾之 ...
- momentum公式
momentum对于w的更新公式: http://caffe.berkeleyvision.org/tutorial/solver.html
- PMD 编译 语法分析 词法分析 抽象语法树
编译原理 163 课堂 http://mooc.study.163.com/learn/-1000002001?tid=1000003000#/learn/content?type=detail&am ...
- 怎样将Oracle数据库设置为归档模式及非归档模式
怎样将Oracle数据库设置为归档模式及非归档模式 1.Oracle日志分类 分三大类: Alert log files--警报日志,Trace files--跟踪日志(用户和进程)和 redo lo ...