智能指针

智能指针的意义

  • 现代 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 = " << pt1;
    qDebug() << "pt2 = " << pt2;

    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 中都提供了对智能指针的支持

以上内容参考狄泰软件学院系列课程,请大家保护原创


TianSong
737 声望139 粉丝

阿里山神木的种子在3000年前已经埋下,今天不过是看到当年注定的结果,为了未来的自己,今天就埋下一颗好种子吧