61、智能指针类模板

智能指针意义:

现代c++开发库中最重要的类模板之一,c++中自动内存管理的主要手段,能够在很大程度上避开内存相关的为题。

stl中智能指针auto_ptr:

生命周期结束时,销毁指向的内存空间。不能指向堆数组,只能指向堆对象(变量)。一片堆空间只属于一个智能指针对象。多个智能指针对象不能指向同一片堆空间。

缺点:不能指向堆数组,只能指向堆对象(变量)。

#include <iostream>
#include <string>
#include <memory>   //智能指针头文件
using namespace std;
class Test
{
    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值为空(地址为0),一片空间只能有一个智能指针
    cout << "pt = " << pt.get() << endl;
    cout << "pt1 = " << pt1.get() << endl;    
    pt1->print();    
    return 0;
}

STL中的其它智能指针

shared_ptr: 带有引用计数机制,支持多个指针对象指向同一片内存。

weak_ptr: 配合shared_ptr而引入的一种智能指针。

扫描二维码关注公众号,回复: 517513 查看本文章

unique_ptr: 一个指针对象指向一片内存空间,不能拷贝构造和赋值。

qt中的智能指针:

    QPointer:当其指向的对象被销毁时,它会被自动置空。析构时不会自动销毁所指向的对象。

QSharedPointer:引用计数型智能指针,可以被自由地拷贝和赋值。当引用计数为0  时才删除指向的对象。

#include <QPointer>
#include <QSharedPointer>
#include <QDebug>
class Test : public QObject
{
    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() << endl;
    QSharedPointer<Test> spt(new Test("Delphi Tang"));
    QSharedPointer<Test> spt1(spt);
    QSharedPointer<Test> spt2(spt);
    spt->print();
    spt1->print();
    spt2->print(); 吃饭
    return 0;

}

自己实现智能指针类模板:根据37课的智能指针

#ifndef _SMARTPOINTER_H_
#define _SMARTPOINTER_H_
template
< typename T >
class SmartPointer
{
    T* mp;
public:
    SmartPointer(T* p = NULL)
    {
        mp = p;
    }
    
    SmartPointer(const SmartPointer<T>& obj)
    {
        mp = obj.mp;
        const_cast<SmartPointer<T>&>(obj).mp = NULL;
    }
    
    SmartPointer<T>& operator = (const SmartPointer<T>& obj)
    {
        if( this != &obj )
        {
            delete mp;
            mp = obj.mp;
            const_cast<SmartPointer<T>&>(obj).mp = NULL;
        }
        
        return *this;
    }
    
    T* operator -> ()
    {
        return mp;
    }
    
    T& operator * ()
    {
        return *mp;
    }
    
    bool isNull()
    {
        return (mp == NULL);
    }
    
    T* get()
    {
        return mp;
    }
    
    ~SmartPointer()
    {
        delete mp;
    }
};
#endif

#include <iostream>
#include <string>
#include "SmartPointer.h"
using namespace std;
class Test
{
    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()
{
    SmartPointer<Test> pt(new Test("D.T.Software"));
    
    cout << "pt = " << pt.get() << endl;
    
    pt->print();
    
    cout << endl;
    
    SmartPointer<Test> pt1(pt);
    
    cout << "pt = " << pt.get() << endl;
    cout << "pt1 = " << pt1.get() << endl;
    
    pt1->print();
    
    return 0;
}
智能指针是c++中自动内存管理的主要手段,智能指针在各种平台上都有不同的表现形式,智能指针能够尽可能的避开内存相关的问题,stl和qt中都提供了对智能指针的支持。

猜你喜欢

转载自blog.csdn.net/ws857707645/article/details/80281680