#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++智能指针实现的更多相关文章

  1. enote笔记法使用范例(2)——指针(1)智能指针

    要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...

  2. C++11 shared_ptr 智能指针 的使用,避免内存泄露

    多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...

  3. C++智能指针

    引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...

  4. EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针

    一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...

  5. 智能指针shared_ptr的用法

    为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...

  6. 智能指针unique_ptr的用法

    unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...

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

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

  8. C++ 引用计数技术及智能指针的简单实现

    一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...

  9. C++11智能指针读书笔记;

    智能指针是一个类对象,而非一个指针对象. 原始指针:通过new建立的*指针 智能指针:通过智能指针关键字(unique_ptr, shared_ptr ,weak_ptr)建立的指针 它的一种通用实现 ...

  10. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

随机推荐

  1. 关于ev||event事件对象的兼容写法

    因为FireFox Chrome默认都是有一个值传进来的,所以这里是对IE和FireFox Chrome做了兼容. 例如:页面点击事件事件要使用document,获取鼠标位置 document.onc ...

  2. 编译错误you should not run configure as root (set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check)

    解决方法: export FORCE_UNSAFE_CONFIGURE=1

  3. 宿主机Windows访问虚拟机Linux文件(二)

    上一篇文章中详细讲述FTP服务(基于文件传输协议的服务),本文则介绍另一种能够实现此功能Telnet(Telecommunications network 远程登陆)服务.本文介绍的telnet我常用 ...

  4. vmware 虚机NAT模式,局域网可访问

    本地VMware虚拟机,网络模式为NAT,现在需要局域网其他电脑通过ssh连接这台VMware虚拟机 宿主机地址:192.168.3.26 VMware虚拟机地址:192.168.239.137 局域 ...

  5. POJ 1185 炮兵阵地 (状压DP,轮廓线DP)

    题意: 给一个n*m的矩阵,每个格子中有'P'或者'H',分别表示平地和高原,平地可以摆放大炮,而大炮的攻击范围在4个方向都是2格(除了自身位置),攻击范围内不能有其他炮,问最多能放多少个炮?(n&l ...

  6. CF Gym 100463B Music Mess (思路)

    好题,当时想了半个小时,我往图论方面去想了,把出现过的字符串当场点,然后相互连边,那么就构成了一个三角形,一个大于三个点的连通分量里有以下结论:度为二的点可能是track,度为大于二的点一定不是tra ...

  7. Sequence II

    6990: Sequence II 时间限制: 3 Sec  内存限制: 128 MB提交: 206  解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 We define an ...

  8. 2018.4.19 远程服务器重装系统之后ssh无法登陆问题

    当我们重装云服务器系统的时候输入ssh连接命令(ssh dc2-user@116.85.25.15)出现一下代码 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  9. 2018.2.27 JavaScript数组方法应用

    JavaScript数组方法应用 1.找出元素item在给定数组arr中的位置 function indexOf(arr,item){ return arr.indexOf(item); } func ...

  10. inner join 和 left join 的区别

    1.left join.right join.inner join的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表 ...