zoukankan      html  css  js  c++  java
  • 再论智能指针(上)

    0.引言:

    目的:使用智能指针替代LinkList中的原生的指针,

    限制:但是我们的智能指针设定了只能单个指针指向某一片堆空间

    1. 智能指针的继承层次结构

    (1)Pointer是智能指针的抽象父类(模板)

      ①纯虚析构函数:virtual ~Pointer()=0;

      ②重载operator->()

      ③重载operator*()

     (2)类的声明

     1 template <typename T>
     2 class Pointer : public Object
     3 {
     4 protected:
     5     T* m_pointer;
     6 public:
     7     Pointer(T* p = NULL);
     8     T* operator->();
     9     T& operator*();
    10     bool isNull();
    11     T* get();
    12    virtual ~Pointer()=0; 
    13 };

    2. SmartPointer的问题

    (1)指针生命周期结束时主动释放堆空间

    (2)一片堆空间最多只能由一个指针标识

    (3)杜绝指针运算和指针比较

     3.【编程实验】

    智能指针的新方案

    //Pointer.h

     

    #ifndef _POINTER_H_
    #define _POINTER_H_
    #include "Object.h"
    
    namespace DataStructureLib
    {
        template <typename T>
        class Pointer : public Object//Pointer不需实现析构函数 Object是析构函数本身是纯虚函数!
        {
        protected:
            T* m_pointer;
        public:
            Pointer(T* p = NULL)
            {
                m_pointer=p;
            }
    
            T* operator->()
            {
                return m_pointer;
            }    
            T& operator*()
            {
                return *m_pointer;
            }
            
            bool isNull()
            {
                return (m_pointer==NULL);
            }
        
            T* get()
            {
                return *m_pointer;
            }
            //virtual ~Pointer()=0;//Pointe可以省略这个析构函数 Object是析构函数本身是纯虚函数!,如果这里不提供析构函数的实现(编译器默认会有析构函数),这个Pointer类一直是个纯虚类
        };
    }
    #endif

    //SmartPointer.h

     1 #ifndef _SMARTPOINTER_H_
     2 #define _SMARTPOINTER_H_
     3 
     4 #include "Pointer.h"
     5 
     6 namespace DataStructureLib {
     7 
     8     //智能指针
     9     template<typename T>
    10     class SmartPointer : public Pointer<T>
    11     {
    12     public:
    13         //构造函数  直接调用父类的构造函数
    14         SmartPointer(T* p = NULL):Pointer(p)
    15         {
    16 
    17         }
    18 
    19          //拷贝构造函数
    20         SmartPointer(const SmartPointer<T>& obj)
    21         {
    22             this->m_pointer=obj->m_pointer;
    23 
    24             //所有权转移,使得同一时刻只能由一个指针指向堆空间
    25             const_cast<SmartPointer<T> &>(obj).m_pointer=NULL;
    26         }
    27 
    28         SmartPointer<T>& operator=(const SmartPointer<T>& obj)
    29         {
    30             if (this!=&obj)
    31             {
    32                 T* p=this->m_pointer;
    33                 this->m_pointer=obj.m_pointer;
    34                 //所有权转移
    35                 const_cast<SmartPointer<T>&>(obj).m_pointer = NULL;
    36                 delete p;
    37             }
    38             return *this;
    39         }
    40         
    41         ~SmartPointer()
    42         {
    43             delete m_pointer;
    44         }
    45     };
    46 }
    47 
    48 
    49 #endif

    测试代码:

     1 #include<iostream>
     2 #include "object.h"
     3 #include "SeqList.h"
     4 #include "LinkList.h"
     5 #include "SmartPointer.h"
     6 using namespace std;
     7 using namespace DataStructureLib;
     8 
     9 class Test : public Object
    10 {
    11 public:
    12     Test()
    13     {
    14         cout << "Test()" << endl;
    15     }
    16 
    17     ~Test()
    18     {
    19         cout <<"~Test()" << endl;
    20     }
    21 };
    22 int main(int argc, char const *argv[])
    23 {
    24     SmartPointer<Test> sp = new Test();
    25     SmartPointer<Test> spn;
    26     spn = sp;
    27     
    28     //system("pause"); 
    29     return 0;
    30 }

    结果

  • 相关阅读:
    二级指针内存模型(二)
    Winserver-FailoverCluster验证异常
    IIS-This configuration section cannot be used at this path.
    SQL SERVER-Extendevent捕获堵塞
    SQL SERVER-Extendevent
    Powershell-加域脚本
    SQL SERVER-端口Port
    WinServer-SMTP服务
    Linux-开机启动程序
    SQL SERVER-修改服务器名称
  • 原文地址:https://www.cnblogs.com/zhaobinyouth/p/9762141.html
Copyright © 2011-2022 走看看