智能指针类

本智能指针模板由三个头文件组成:Pointer.h   SmartPointer.h  SharedPointer.h

 Pointer.h实现:指针变量,构造函数来初始化模板成员变量,操作符的重载,非空判断。

SmartPointer.h实现:拷贝构造函数,赋值符的重载,析构函数释放堆空间

         1. 指针生命周期结束时自动释放堆空间

         2. 一个堆空间只由一个智能指针指向

         3. 禁止指针运算和指针比较

 SharedPointer.h实现:

         1. 实现多个指针指向同一个堆空间,最后一个指针生命周期结束时释放堆空间。(通过计数机制标识ref堆空间,堆空间被指向时ref++,指针被置空时ref--,ref==0时释放堆空间)

         2. 禁止指针运算和指针比较。

三者继承关系如图:

Pointer.h

/*
*    Pointer: 智能指针
*    模板成员:
*            pointer
*    模板函数:
*            Pointer()    构造函数初始化指针
*            operator ->    
*            operator *    
*            isNull()    指针非空
*            get()        获取指针变量值
*/

#ifndef POINTER_H
#define POINTER_H

#include"TopClass.h"

namespace DSL
{
    template <typename T>
    class Pointer : public TopClass
    {
        protected:
                T* m_pointer;
        public:
                Pointer(T* ptr = NULL)
                {
                    m_pointer = ptr;
                }
                
                T* operator -> ()
                {
                    return m_pointer;
                }
                
                T& operator * ()
                {
                    return *m_pointer;
                }
                
                const T* operator -> () const // const重载
                {
                    return m_pointer;
                }
                
                const T& operator * () const // const重载
                {
                    return *m_pointer;
                }
                
                bool isNull() const
                {
                    return m_pointer == NULL;
                }
                
                T* get() const
                {
                    return m_pointer;
                }
                
    };

}

#endif
View Code

 SmartPointer.h

/* SmartPoniter: 智能指针模板
 * 1.指针生命周期结束时主动释放堆空间 --> 析构函数
 * 2.一个堆空间只由一个指针指向        --> 拷贝构造函数,重载赋值符函数
 * 3.不可进行指针运算 -->
 * 4.重载解指针操作符,成员访问符  --> 重载->和*函数
 * 5.指针初始化为NULL --> 构造函数
 * 6.得到指针地址 --> get函数
 * 7.判断指针非空 --> isNULL函数
 */
 
#ifndef  SMARTPOINTER_H
#define  SMARTPOINTER_H

#include<iostream>
#include"Pointer.h"


namespace DSL
{
    template <typename T>
    class SmartPoniter : public Pointer<T>
    {
    public:
        SmartPoniter(T* p = NULL) : Pointer<T>(p) // 调用父类构造函数
        {    }
        
        SmartPoniter(const SmartPoniter<T>& obj) 
        {
            this->m_pointer = obj.m_pointer;
            const_cast<SmartPoniter<T>&>(obj).m_pointer = NULL;
        }
        
        SmartPoniter<T>& operator = (const SmartPoniter<T>& obj)  
        {
             if(this != &obj)
               {
                   T* temp = this->m_pointer;
                this->m_pointer = obj.m_pointer;
                const_cast<SmartPoniter<T>&>(obj).m_pointer = NULL;
                delete temp; // 异常安全
            }
            return *this;
        }
        
        ~SmartPoniter() 
        {
            delete this->m_pointer;
        }

    };
}

#endif
View Code

SharedPointer.h

/*
*    SharedPointer:    共享指针模板
*    成员变量:
*            m_ref:    计数指针
*    成员函数:
*            SharedPointer:    給计数指针申请堆空间,初始化m_pointer,m_ptr。计数加一。
*            SharedPointer:    设置m_ref,m_pointer。计数加一。
*            operator =   :  清除左值对象的成员变量,重新赋值。计数加一。
*            clear()         :  清除对象成员变量,判断计数是否为零。
*            
*            operator ==
*           operator !=
*/
#ifndef SHAREDPOINTER_H
#define SHAREDPOINTER_H

#include"Pointer.h"
#include"Exception.h"
#include<cstdlib>

namespace DSL
{
    template < typename T >
    class SharedPointer : public Pointer<T>
    {
    protected:
            int* m_ref;
    public:
            SharedPointer(T* ptr = NULL) : m_ref(NULL)
            {
                if(ptr != NULL)
                {
                    this->m_ref = static_cast<int*>(std::malloc(sizeof(int)));
                    if(this->m_ref != NULL)
                    {
                        this->m_pointer = ptr;
                        *(this->m_ref) = 1;
                    }
                    else
                    {
                        THROW_EXCEPTION(NotEnoughMemoryException,"error: no enougth memory to create count reference!");
                    }
                }
            }
            
            SharedPointer(const SharedPointer<T>& obj)
            {
                this->m_ref = obj.m_ref;
                this->m_pointer = obj.m_pointer;
                if( this->m_ref != NULL )
                {
                    *(this->m_ref)++;
                }
            }

            SharedPointer<T>& operator = (const SharedPointer<T>& obj)
            {
                if(this != &obj)
                {
                    clean();
                    this->m_pointer = obj.m_pointer;
                    this->m_ref = obj.m_ref;
                    if(this->m_pointer != NULL)
                    {
                        *(this->m_ref)++;
                    }
                }
                return *this;
            }

            void clean()
            {
                T* temp_pointer = this->m_pointer;
                int* temp_ref = this->m_ref;
                this->m_pointer = NULL;
                this->m_ref = NULL;
                if(temp_ref)
                {
                    *temp_ref--;
                    if(*temp_ref == 0)
                    {
                        std::free(temp_ref);
                        delete temp_pointer;
                    }
                }
            }
    };

template <typename T>

    bool operator == (const SharedPointer<T>& obj1,const SharedPointer<T>& obj2)
    {
        return (obj1.get() == obj2.get());
    }

template <typename T>

    bool operator != (const SharedPointer<T>& obj1,const SharedPointer<T>& obj2)
    {
        return !(obj1 == obj2);
    }

}

#endif
View Code
原文地址:https://www.cnblogs.com/zsy12138/p/11050144.html