1、再论智能指针
课程目标
- 完成SharedPointer类的实现
SharedPointer类的设计要点
-类模板
通过计数机制( ref )标识堆内存
堆内存被指向时: ref++
指针被置空时: ref--
ref == 0时:释放堆内存-
计数机制原理剖析
SharedPointer类的声明
智能指针的比较
由于SharedPointer支持多个对象同时指向
一片堆空间;因此,必须支持比较操作!
2、编程实验
智能指针的新成员 SharedPointer.h- #ifndef SHAREDPOINTER_H
- #define SHAREDPOINTER_H
- #include"Exception.h"
- #include"Pointer.h"
- #include<cstdlib>
- namespace DTLib
- {
- template <typename T>
- class SharedPointer : public Pointer<T>
- {
- protected:
- int* m_ref;
- void assign(const SharedPointer<T>& obj)
- {
- this->m_ref = obj.m_ref;
- this->m_pointer = obj.m_pointer;
- if(this->m_ref)
- {
- *(this->m_ref)++;
- }
- }
- public:
- SharedPointer(T* p = NULL) : m_ref(NULL)
- {
- if(p)
- {
- this->m_ref = static_cast<int*>((std::malloc(sizeof(int))));
- if(this->m_ref)
- {
- *(this->m_ref) = 1;
- this->m_pointer = p;
- }
- else
- {
- THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create SharedPointer object ...");
- }
- }
- }
- SharedPointer(const SharedPointer<T>& obj): Pointer<T>(NULL)
- {
- assign(obj);
- }
- SharedPointer<T>& operator = (const SharedPointer<T>& obj)
- {
- if(this != &obj)
- {
- clear(); //当前智能指针对象不指向任何堆空间
- assign(obj);
- }
- return *this;
- }
- void clear()
- {
- T* toDel = this->m_pointer;
- int* ref = this->m_ref;
- this->m_pointer = NULL;
- this->m_ref = NULL;
- if(ref)
- {
- (*ref)--; //智能指针对象不再指向堆空间,计数变量减一
- if(*ref == 0)
- {
- free(ref);
- delete toDel;
- }
- }
- }
- ~SharedPointer()
- {
- clear();
- }
- };
- template <typename T>
- bool operator == (const SharedPointer<T>& l,const SharedPointer<T>& r)
- {
- return (l.get() == r.get());
- }
- template <typename T>
- bool operator != (const SharedPointer<T>& l,const SharedPointer<T>& r)
- {
- return !(l == r); //使用相等操作符
- }
- }
- #endif // SHAREDPOINTER_H
因此必须修改Pointer.h
- #ifndef POINTER_H
- #define POINTER_H
- #include"Object.h"
- namespace DTLib
- {
- template <typename T>
- class Pointer : public 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;
- }
- bool isNull() const
- {
- return m_pointer == NULL;
- }
- T* get() const
- {
- return m_pointer;
- }
- //只要不实现析构函数,就是抽象类(继承了顶层父类)
- };
- }
- #endif // POINTER_H
main.cpp
#include <iostream>
#include"SharedPointer.h"
using namespace std;
using namespace DTLib;
class Test : public Object
{
public:
int value;
Test():value(0)
{
cout<<"Test"<<endl;
}
~Test()
{
cout<<"~Test"<<endl;
}
};
int main()
{
SharedPointer<Test> sp0 = new Test();
SharedPointer<Test> sp1 = sp0;
SharedPointer<Test> sp2 ;
sp2 = sp1;
sp2->value = 20;
cout<<sp0->value<<endl;
cout<<sp1->value<<endl;
cout<<sp2->value<<endl;
cout<<(sp0 == sp2)<<endl;
sp2.clear();
cout<<(sp0 == sp2)<<endl;
return 0;
}
智能指针的使用军规
-只能用来指向堆空间中的单个变量(对象)
-不同类型的智能指针对象不能混合使用
-不要使用 delete释放智能指针指向的堆空间3、小结
SharedPointer最大程度的模拟了原生指针的行为
计数机制确保多个智能指针台法的指向同—片堆空间
智能指针只能用于指向堆空间中的内存
不同类型的智能指针不要混台使用
堆对象的生命周期由智能指针进行管理