• 智能指针的用处:在c++中,使用普通指针容易造成堆内存的泄露问题,即程序员会忘记释放,以及二次释放,程序发生异常时内存泄漏等问题,而使用智能指针可以更好的管理堆内存。注意,在这里智能指针是一个类而非真正的指针,只是对一个真正的指针进行包装,代理原指针。通过操作符的重载,可以让智能指针和真正的指针有类似的操作。
  • 如何实现:在智能指针析构函数当中,可以对代理的原指针进行delete以及赋空指针等操作,智能指针销毁时,自动调用析构函数,省去了程序员自己管理指针的操作。当多个智能指针代理同一个指针时,我们要引入一个计数器,用来计数智能指针的个数,最后一个销毁的智能指针有义务对原指针进行销毁工作,具体实现代码如下:
template<class T>
class Smart_ptr {
public:
Smart_ptr(T*ptr);
Smart_ptr(const Smart_ptr &ptr);//拷贝构造函数
~Smart_ptr();//析构函数
int get_cnt();//获得当前代理原指针的智能指针个数
Smart_ptr& operator=(const Smart_ptr&ptr);//赋值操作
T& operator *();//指针操作
T* operator ->();//指针操作
private:
T*_ptr;
int*_cnt;
};
template<class T>
Smart_ptr<T>::Smart_ptr(T*ptr):_ptr(ptr) {//判断参数指针是否为空;不为空,计数+1
if (_ptr) _cnt = new int();
else _cnt = new int();
}
template<class T>
Smart_ptr<T>::Smart_ptr(const Smart_ptr &ptr) {//复制构造函数,复制成功,计数加1
if (this != &ptr) {
this->_ptr = ptr._ptr;
this->_cnt = ptr._cnt;
(*this->_cnt)++;
}
}
template<class T>
Smart_ptr<T>::~Smart_ptr() {//若当前的智能指针是最后一个代理指针,负责销毁原指针
(*this->_cnt)--;
if ((*this->_cnt) == ) {
delete this->_cnt;
delete this->_ptr;
_cnt = nullptr;
_ptr = nullptr;
}
}
template<class T>
Smart_ptr<T>& Smart_ptr<T>::operator=(const Smart_ptr&ptr) {//1:若赋值前后代理的指针是一样的,直接返回即可2:先判断当前的智能指针是否为最后一个代理原指针的智能指针,若是,销毁原指针;
if (this->_ptr == ptr._ptr)return *this;//3:当前智能指针将代理一个新的指针
if (this->_ptr != nullptr) {
(*this->_cnt)--;
if (*this->_cnt == ) {
delete this->_cnt;
delete this->_ptr;
}
}
this->_cnt = ptr._cnt;
this->_ptr = ptr._ptr;
(*this->_cnt)++;
return *this;
}
template<class T>
T& Smart_ptr<T>::operator *() {//指针操作
return *(this->_ptr);
}
template<class T>
T* Smart_ptr<T>::operator->() {//指针操作
return this->_ptr;
}
template<class T>
int Smart_ptr<T>::get_cnt() {
return *this->_cnt;
}

main函数:

int main() {
Smart_ptr<int>sp1(new int());
cout <<"sp1.cnt:"<< sp1.get_cnt() << endl;
Smart_ptr<int>sp2(sp1);
cout << "sp1.cnt:" << sp1.get_cnt() << endl;
Smart_ptr<int>sp3(new int());
cout << "sp3.cnt:" << sp3.get_cnt() << endl;;
sp3 = sp2;
cout << "sp3.cnt:" << sp3.get_cnt() << endl;
return ;
}

调用结果:

C++ 智能指针的简单实现的更多相关文章

  1. 【C++】智能指针auto_ptr简单的实现

    //[C++]智能指针auto_ptr简单的实现 #include <iostream> using namespace std; template <class _Ty> c ...

  2. C++智能指针及其简单实现

    本文将简要介绍智能指针shared_ptr和unique_ptr,并简单实现基于引用计数的智能指针. 使用智能指针的缘由 1. 考虑下边的简单代码: int main() { ); ; } 就如上边程 ...

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

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

  4. C++ 拷贝控制和资源管理,智能指针的简单实现

    C++ 关于拷贝控制和资源管理部分的笔记,并且介绍了部分C++ 智能指针的概念,然后实现了一个基于引用计数的智能指针.关于C++智能指针部分,后面会有专门的研究. 通常,管理类外资源的类必须定义拷贝控 ...

  5. C++智能指针

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

  6. 智能指针auto_ptr & shared_ptr

    转载:智能指针auto_ptr 很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生 ...

  7. STL 智能指针

    转自: https://blog.csdn.net/k346k346/article/details/81478223 STL一共给我们提供了四种智能指针:auto_ptr.unique_ptr.sh ...

  8. C++ auto_ptr智能指针的用法

    C++中指针申请和释放内存通常采用的方式是new和delete.然而标准C++中还有一个强大的模版类就是auto_ptr,它可以在你不用的时候自动帮你释放内存.下面简单说一下用法. 用法一: std: ...

  9. C++智能指针的几种用法

    auto在c++11中已经弃用. 一.auto_ptr模板 auto_ptr与shared_ptr.unique_ptr都定义了类似指针的对象,可以将new到的地址赋给这一对象,当智能指针过期时,析构 ...

随机推荐

  1. jmeter设置全局变量的方法

    需求: 同一个线程组内有两个http请求A.B,A请求的后置处理器中存储的有值,B请求中添加用户变量Va先要引用该值,然后B请求的前置处理器再引用用户变量va. 第一种方式: 1.A请求后置处理添加如 ...

  2. VS2010使用NuGet程序包管理器

    使用C#过程中经常需要使用一些扩展包,例如sqlite,json解析等. VS2010自带了一个扩展管理器,里面可以下载到AStyle,Visual Assit等有用的插件. VS2010中点击[工具 ...

  3. 了解url

    我对自己知道关于url的编码和解码的一些进行了一下整理. 我们的例子是百度翻译的地址: http://fanyi.baidu.com/translate#en/zh/The%20%22%3F%20ar ...

  4. HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)

    Problem Description Mex is a function on a set of integers, which is universally used for impartial ...

  5. DFS做题小结

    一.深入理解DFS 采用递归写法 深度优先,思路就是沿着一条路一直走,直到走到死胡同,原路返回,返回到有多条道路的地方换其他路走.直到这条支路全部都访问过了,按照原路返回,回到起点,如果起点还有别的支 ...

  6. 【转】 The user specified as a definer ('root'@'') does not exist when using LOCK TALBE

    在linux下,用mysql的导出语句: mysqldump -u root -pPasswd table >/home/lsf/test.sql 出现了 Got error: 1449: Th ...

  7. oop &&GP 模板 ---> 特化和偏特化

    OOP面向对象编程 GP泛型编程(generic programming) 两者的主要区别就是OOP将数据和对数据的操作放在一起, GP就是将数据和操作独立开来 GP:   数据就是container ...

  8. User Agent的学习

    什么是User-Agent? User-Agent是一个特殊字符串头,被广泛用来标示浏览器客户端的信息,使得服务器能识别客户机使用的操作系统和版本,CPU类型,浏览器及版本,浏览器的渲染引擎,浏览器语 ...

  9. A1

    It’s surprising what you can find at the end of your garden. Wild flowers... and even smaller yet, i ...

  10. To Chromium之版本管理

    Git. 1.由于想直接submit到Chromium的官方Branch需要申请权限,目前拿不到,所以打算snapshot一个chromium版本. 本地搭建一个git的server/client,方 ...