• 智能指针的用处:在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. 【python3.X】Scrapy学习途径参考

    如何爬取属性在不同页面的itemhttp://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/request-response.html#topics-requ ...

  2. 11 TCP实现QQ聊天

    1.客户端参考代码 #coding=utf-8 from socket import * # 创建socket tcpClientSocket = socket(AF_INET, SOCK_STREA ...

  3. bzoj 一些题目汇总

    2140: 稳定婚姻 /* 求联通分量. */ #include<bits/stdc++.h> using namespace std; typedef long long LL; inl ...

  4. Can’t delete list item in Sharepoint2013

         Today,I have meet a very strange error.When I attempt to delete a item from a list,I recieve an ...

  5. 部署:阿里云ECS部署Docker CE

    1 部署阿里云ECS,选择CentOS操作系统,并启动实例: 2 部署Docker CE: a.检查centos版本: $ cat /etc/redhat-release CentOS Linux r ...

  6. Qt QPainter::end: Painter ended whith 2 saced states

    在使用Qt  QPainter 的时候,有时会遇到“QPainter::end: Painter ended whith 2 saced states” 这时由于我们在使用的QPanter.trans ...

  7. 6.0 实现app登录

    1.0.0:学习ui自动化准备工作 待测app,我这里有准备两个apk,这两个都是我曾经做过的项目,后续的文章都是基于这两个app! 链接:https://pan.baidu.com/s/1I0vR9 ...

  8. Halcon17无法加载"hdevenginecpp":找不到指定的模块

    Halcon17无法加载"hdevenginecpp":找不到指定的模块 在C#和Halcon17混合编程中,当执行private HDevEngine MyEngine = ne ...

  9. python类学习以及mro--多继承属性查找机制

    版权声明:本文为博主原创文章,未经博主允许不得转载. 还记得什么是新式类和旧式类吗? Python中,一个class继承于object,或其bases class里面任意一个继承于object,这个c ...

  10. 使用 window.getSelection() 方法获取鼠标划取部分的起始位置和结束位置的问题(高亮后不能正确获取)

    如果没有高亮等复杂处理,只需要获取一段文字中选取的字和位置,那么 使用window.getSelection()获取div中选中文字内容及位置 怎么获取textarea中选中文字 则可以满足需求: - ...