• 智能指针的用处:在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. Windows下使用PHP Xdebug

    首先下载Xdebug的dll:http://xdebug.org/download.php 将dll文件放到php目录下的ext目录里面: 修改php.ini,根据自己的需要增加信息: [Xdebug ...

  2. VM打开虚拟机文件报错

    用VM打开以前的虚拟机文件报错 Cannot open the disk 'F:/****.vmdk' or one of the snapshot disks it depends on. 这种问题 ...

  3. windows 10 下的linux子系统用法 -- tmux分屏工具用法

    1 激活linux子系统的方法见百度: 2 打开powershell,输入bash启动子系统终端:输入exit退出: 3 输入tmux attach连接会话:ctrl-b+d 返回终端:ctrl-b+ ...

  4. Spring MVC 开发 配置

    1.首先在web.xml中配置一个DispatcherServlet,并通过<servlet-mapping>指定需要拦截的url. 下面xml中配置一个拦截.html为后缀的url. & ...

  5. Sublime text3最全快捷键清单

    [转]https://blog.csdn.net/mrchengzp/article/details/78508509,感谢作者的分享,收录方便查阅   Sublime Text 支持多种编程语言的语 ...

  6. 树莓派3_win10下使用"远程桌面连接"与树莓派通信(使用VNC实现连接后)

    -----------------------------------------------------------学无止境------------------------------------- ...

  7. Paper Reading - Show and Tell: Lessons learned from the 2015 MSCOCO Image Captioning Challenge

    Link of the Paper: https://arxiv.org/abs/1609.06647 A Correlative Paper: Show and Tell: A Neural Ima ...

  8. HDU 4782 Beautiful Soup (模拟+注意细节)

    思路就是用栈模拟,不用开实体的栈,直接记一个top指针就行. 说说这题的细节: 1.tag标签里的内容不要动,原样输出.比如<p aa bb cc>,就这样输出就行,不要删空格.题目中说了 ...

  9. URAL 1932 The Secret of Identifier(容斥)

    Description Davy Jones: You've been captain of the Black Pearl for 13 years. That was our agreement. ...

  10. cocos2d-x环境搭建 摘自百度文库

    cocos2d-x环境搭建 引言:笔者在网上寻觅了很多资料,最终发现了这份实际可用的文档,供大家参考.源地址:http://wenku.baidu.com/view/93f7b0f1102de2bd9 ...