Pointer
智能指针的特点:
智能指针可以像指针一样使用(重载->和*)
智能指针生命周期结束自动释放内存空间
智能指针只能指向堆空间中的内存
禁止使用Delete释放智能指针指向的对象
SmartPointer的特点:
一片堆空间只可以属于一个智能指针对象
拷贝构造或者赋值操作时,原指针置NULL(若新指针原不为NULL,释放指向的对象)新指针指向新对象
可以用于多个指针指向同一个对象的场合
SharedPointer的特点:
一片堆空间可以被多个智能指针对象指向
拷贝构造或者赋值操作时,计数+1,(若新指针原不为NULL,原对象的计数-1)新指针指向新对象
如果计数减为0,才会释放指向的对象
可以用于多个指针指向同一个对象的场合
QPointer (Qt中)的特点:
析构时 不会自动销毁所指向的对象
当其指向的对象被销毁时,它会被自动 置NULL
可用于QtGUI开发松耦合的场合
![](https://i-blog.csdnimg.cn/blog_migrate/0cfa162f8be0ee6deb9bd7c4c2db8d43.png)
定义顶层抽象类,重载指针特征操作符
Pointer.h
#ifndef _Pointer_H_
#define _Pointer_H_
#include "include/CPlusPlus.h"
#include "include/Object.h"
namespace JYlib
{
template< typename T>
class Pointer:public Object//没有完成Object的虚函数—析构函数,所以是抽象类
{
protected: //可以被继承
T* m_pointer;
public:
Pointer(T* p = NULL)
{
m_pointer = p;
}
T& operator *()
{
return (*m_pointer);
}
T* operator ->()
{
return m_pointer;
}
const T& operator *()const
{
return (*m_pointer);
}
const T* operator ->()const
{
return m_pointer;
}
T* get()const
{
return m_pointer;
}
bool IsNull()const
{
return (m_pointer == NULL);
}
};
}
#endif
SmartPointer
SmartPointer.h
#ifndef _SmartPointer_H_
#define _SmartPointer_H_
#include "include/Pointer.h"
namespace JYlib
{
template< typename T>
class SmartPointer:public Pointer<T>//智能指针会自动释放堆空间,只能应用于指向堆空间的对象
{
protected: //可以被继承
public:
SmartPointer(T* p = NULL):Pointer<T>(p)
{
}
SmartPointer(const SmartPointer<T> &obj)//原本的指针置空,同一内存只能一个指针指向
{
this->m_pointer = obj.m_pointer;
const_cast<SmartPointer<T>&>(obj).m_pointer = NULL;
}
SmartPointer<T>& operator =(const SmartPointer<T> &obj)//原本的内存析构,指向新内存,并让原指针置空
{
if(this != &obj)
{
T* remove = this->m_pointer;//异常安全
this->m_pointer = obj.m_pointer;
const_cast<SmartPointer<T>&>(obj).m_pointer = NULL;
delete remove;
}
return *this;
}
~SmartPointer()
{
delete this->m_pointer;
}
};
}
#endif
SharedPointer
利用计数机制,和重载比较操作符,来判断是否被多个智能指针指向
SharedPointer.h
#ifndef _SharedPointer_H_
#define _SharedPointer_H_
#include "Pointer.h"
#include "Exception.h"
#include <cstdlib>
namespace JYlib
{
/*
构造时创建一个内存空间,用指针指向它
拷贝构造时,创建的指针直接指向对方的内存空间
*/
template< typename T>
class SharedPointer:public Pointer<T>//智能指针会自动释放堆空间,只能应用于指向堆空间的对象
{
protected: //可以被继承
int* m_ref;//计数机制成员指针
void assign(const SharedPointer<T> &obj)
{
this->m_pointer = obj.m_pointer;
m_ref = obj.m_ref;
if(m_ref)
{
(*m_ref)++;
}
}
public:
SharedPointer(T* p = NULL):Pointer<T>(p),m_ref(NULL)
{
if(p)
{
m_ref = static_cast<int*>(malloc(sizeof(int)));
if(this->m_ref)
{
*m_ref = 1;
this->m_pointer = p;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create SharedPointer object ...");
}
}
}
//清除原内容,指向新内容,内容有效,计数加1
SharedPointer(const SharedPointer<T> &obj):Pointer<T>(obj.m_pointer)
{
assign(obj);
}
//清除原内容,指向新内容,内容有效,计数加1
SharedPointer<T>& operator =(const SharedPointer<T> &obj)
{
if(this != &obj)
{
clear();
assign(obj);
}
return *this;
}
bool operator ==(const SharedPointer<T> &obj)
{
return (this->get() == obj.get());
}
bool operator !=(const SharedPointer<T> &obj)
{
return (this->get() != obj.get());
}
void clear()
{
if(this->m_pointer != NULL)
{
T* rem_pointer = this->m_pointer;//异常安全
int* rem_ref = m_ref;
this->m_pointer = NULL;
m_ref = NULL;
(*rem_ref)--;
if((*rem_ref) == 0)
{
delete rem_pointer;
delete rem_ref;
}
}
}
~SharedPointer()
{
clear();
}
};
}
#endif