C++ 智能指针的简单实现
- 智能指针的用处:在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++ 智能指针的简单实现的更多相关文章
- 【C++】智能指针auto_ptr简单的实现
//[C++]智能指针auto_ptr简单的实现 #include <iostream> using namespace std; template <class _Ty> c ...
- C++智能指针及其简单实现
本文将简要介绍智能指针shared_ptr和unique_ptr,并简单实现基于引用计数的智能指针. 使用智能指针的缘由 1. 考虑下边的简单代码: int main() { ); ; } 就如上边程 ...
- C++ 引用计数技术及智能指针的简单实现
一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...
- C++ 拷贝控制和资源管理,智能指针的简单实现
C++ 关于拷贝控制和资源管理部分的笔记,并且介绍了部分C++ 智能指针的概念,然后实现了一个基于引用计数的智能指针.关于C++智能指针部分,后面会有专门的研究. 通常,管理类外资源的类必须定义拷贝控 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- 智能指针auto_ptr & shared_ptr
转载:智能指针auto_ptr 很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生 ...
- STL 智能指针
转自: https://blog.csdn.net/k346k346/article/details/81478223 STL一共给我们提供了四种智能指针:auto_ptr.unique_ptr.sh ...
- C++ auto_ptr智能指针的用法
C++中指针申请和释放内存通常采用的方式是new和delete.然而标准C++中还有一个强大的模版类就是auto_ptr,它可以在你不用的时候自动帮你释放内存.下面简单说一下用法. 用法一: std: ...
- C++智能指针的几种用法
auto在c++11中已经弃用. 一.auto_ptr模板 auto_ptr与shared_ptr.unique_ptr都定义了类似指针的对象,可以将new到的地址赋给这一对象,当智能指针过期时,析构 ...
随机推荐
- bzoj 一些题目汇总
2140: 稳定婚姻 /* 求联通分量. */ #include<bits/stdc++.h> using namespace std; typedef long long LL; inl ...
- 50道基础的java面试题
Java程序员面试题集(1-50) 一.Java基础部分 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 1)抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象 ...
- c++ combination by next_permutation
#include <iostream> #include <algorithm> #include <vector> int main() { int n, r; ...
- vue2.0 $emit $on组件通信
在vue1.0中父子组件通信使用$dispatch 和 $broadcast,但是在vue2.0中$dispatch 和 $broadcast 已经被弃用. 因为基于组件树结构的事件流方式实在是让人难 ...
- jmeter接口测试--参数化
接口测试时遇到一些属性不能重复时,可以使用Random 随机函数,除此之外,也可以用用户参数 一..随机参数化 1.在jmeter工具,菜单-选项-函数助手对话框,输入数值,属性,点击生成: 2.在相 ...
- React错误总结(三)
神坑react native之Cannot Add a child that doesn't have a YogaNode to a parent with out a measure functi ...
- Visual Studio各版本工程文件之间的转换 [转载]
原网址:http://www.cnblogs.com/jmliao/p/5594179.html Visual Studio各版本工程文件之间的转换 由于VS版本比较多,低版本无法直接打开高版本的 ...
- BZOJ 3597 SCOI2014 方伯伯送椰子 网络流分析+SPFA
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3597 Description 四川的方伯伯为了致富,决定引进海南的椰子树.方伯伯的椰子园十 ...
- 基于bootstrap动态分页
bootstrap本身的分页有分页组件 但是却是静态的,无法满足要求,分页必须根据当前的总页数来展示 使用插件bootstrap-paginator github下载地址 https://github ...
- 时间戳转换成日期的js
在项目开发过程中,我们常常需要把时间戳转换成日期.下面这个是我一直使用的js方法,希望能帮助到有需要的朋友.大家如果有更好的方法,请多多指教! js代码如下: //时间戳转换成日期 function ...