【C++】 61_智慧指標類别範本

TianSong發表於2019-05-13

智慧指標

智慧指標的意義

  • 現代 C++ 開發庫中最重要的類别範本之一
  • C++ 中記憶體管理的主要手段
  • 能夠在很大程度上避開記憶體相關的問題

    • 記憶體洩漏
    • 記憶體多次釋放

STL 中的智慧指標 auto_ptr

  • 生命週期結束時,銷燬指向的記憶體(防止記憶體洩漏)
  • 不能指向堆陣列,只能指向堆物件(變數)
  • 一片堆空間只屬於一個智慧指標物件(防止記憶體多次釋放)
  • 多個智慧指標物件不能指向同一片堆空間

程式設計實驗: auto_ptr 使用初探

#include <iostream>
#include <memory>

using namespace std;

class Test
{
private:
    string m_name;
    
public:
    Test(const char* name)
    {
        cout << "Hello, " << name << endl;
        
        m_name = name;
    }
    
    void print()
    {
        cout << "I`m " << m_name << endl;
    }
    
    ~Test()
    {
        cout << "Goodbye, " << m_name << endl;
    }
};

int main()
{
    auto_ptr<Test> pt(new Test("D.T.Software"));
    
    cout << "pt = " << pt.get() << endl;
    
    pt->print();
    
    cout << endl;
    
    auto_ptr<Test> pt1(pt);        // 所有權轉移 : pt ==> pt1
    cout << "pt = " << pt.get() << endl;
    cout << "pt = " << pt1.get() << endl;
    
    pt1->print();
    
    return 0;
}
輸出:
Hello, D.T.Software
pt = 0x90e7008
I`m D.T.Software

pt = 0
pt = 0x90e7008
I`m D.T.Software
Goodbye, D.T.Software

STL 中的其它指標

  • share_ptr

    • 帶有引用計數機制,支援多個指標物件指向同一片記憶體空間
  • weak_ptr

    • 配合 shared_ptr 而引入的一種智慧指標
  • unique_ptr

    • 一個指標物件指向一片記憶體空間,不能拷貝構造和賦值

Qt 中的智慧指標

  • QPointer

    • 當其指向的物件被銷燬時,他會被自動置空(防止記憶體多次釋放)
    • 析構時不會自動銷燬所指向的物件
  • QSharedPointer

    • 引用計數型智慧指標
    • 可以被自由的拷貝和賦值
    • 當引用計數為 0 時才刪除指向的物件(防止記憶體洩漏)

程式設計實驗: Qt中的智慧指標

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QPointer>
#include <QSharedPointer>

class Test : public QObject
{
private:
    QString m_name;

public:
    Test(const char* name)
    {
        qDebug() << "Hello, " << name;

        m_name = name;
    }

    void print()
    {
        qDebug() << "I`m " << m_name;
    }

    ~Test()
    {
        qDebug() << "Goodbye, " << m_name;
    }
};

int main()
{
    QPointer<Test> pt(new Test("D.T.Software"));
    QPointer<Test> pt1(pt);
    QPointer<Test> pt2(pt);

    pt->print();
    pt1->print();
    pt2->print();

    delete pt;

    qDebug() << "pt = " << pt;
    qDebug() << "pt1 = " << pt;
    qDebug() << "pt2 = " << pt;

    qDebug();

    QSharedPointer<Test> spt(new Test("D.T.Software"));
    QSharedPointer<Test> spt1(spt);
    QSharedPointer<Test> spt2(spt);

    spt->print();
    spt1->print();
    spt2->print();

    return 0;
}
輸出:
Hello,  D.T.Software
I`m  "D.T.Software"
I`m  "D.T.Software"
I`m  "D.T.Software"
Goodbye,  "D.T.Software"
pt =  QObject(0x0)
pt1 =  QObject(0x0)
pt2 =  QObject(0x0)

Hello,  D.T.Software
I`m  "D.T.Software"
I`m  "D.T.Software"
I`m  "D.T.Software"
Goodbye,  "D.T.Software"

Qt 中的其它智慧指標

  • QWeakPointer
  • QScopedPointer
  • QScopedArrayPointer
  • QSharedDataPointer
  • QExplicitlySharedDataPointer

程式設計實驗: 建立智慧指標類别範本

SmartPointer.h

#ifndef _SMARTPOINTER_H_
#define _SMARTPOINTER_H_

template
< typename T >
class SmartPoniter
{
private:
    T* m_pointer;
public:
    SmartPoniter(T* p = NULL);
    SmartPoniter(const SmartPoniter<T>& obj);
    SmartPoniter<T>& operator = (const SmartPoniter<T>& obj);
    T* operator -> ();
    T& operator * ();
    bool isNull();
    T* get();
    ~SmartPoniter();
};

template
< typename T >
SmartPoniter<T>::SmartPoniter(T* p)
{
    m_pointer = p;
}

template
< typename T >
SmartPoniter<T>::SmartPoniter(const SmartPoniter<T>& obj)
{
    m_pointer = obj.m_pointer;                    // 所有權轉接
    const_cast<SmartPoniter<T>&>(obj).m_pointer = NULL;
}

template
< typename T >
SmartPoniter<T>& SmartPoniter<T>::operator = (const SmartPoniter<T>& obj)
{    
    if( this != &obj )
    {
        delete m_pointer;                         // 所有權轉接
        m_pointer = obj.m_pointer;
        const_cast<SmartPoniter<T>&>(obj).m_pointer = NULL;        
    }
    
    return *this;
}

template
< typename T >
T* SmartPoniter<T>::operator -> ()
{
    return m_pointer;
}

template
< typename T >
T& SmartPoniter<T>::operator * ()
{
    return *m_pointer;
}

template
< typename T >
bool SmartPoniter<T>::isNull()
{
    return (m_pointer == NULL);
}

template
< typename T >
T* SmartPoniter<T>::get()
{
    return m_pointer;
}

template
< typename T >
SmartPoniter<T>::~SmartPoniter()
{
    delete m_pointer; 
}

#endif

main.cpp

#include <iostream>
#include <memory>
#include "SmartPointer.h"

using namespace std;

class Test
{
private:
    string m_name;
    
public:
    Test(const char* name)
    {
        cout << "Hello, " << name << endl;
        
        m_name = name;
    }
    
    void print()
    {
        cout << "I`m " << m_name << endl;
    }
    
    ~Test()
    {
        cout << "Goofbye, " << m_name << endl;
    }
};

int main()
{
    SmartPoniter<Test> pt(new Test("D.T.Software"));
    
    cout << "pt = " << pt.get() << endl;
    
    pt->print();
    
    cout << endl;
    
    SmartPoniter<Test> pt1(pt);
    cout << "pt = " << pt.get() << endl;
    cout << "pt = " << pt1.get() << endl;
    
    pt1->print();
    
    return 0;
}
輸出:
Hello, D.T.Software
pt = 0x815e008
I`m D.T.Software

pt = 0
pt = 0x815e008
I`m D.T.Software
Goofbye, D.T.Software

小結

  • 智慧指標是 C++ 中自動記憶體管理的主要手段
  • 智慧指標在各種平臺上都有不同的表現形式
  • 智慧指標能夠儘可能的避開記憶體相關的問題
  • STL 和 Qt 中都提供了對智慧指標的支援

以上內容參考狄泰軟體學院系列課程,請大家保護原創

相關文章