C++笔记 第六十一课 智能指针类模板---狄泰学院

如果在阅读过程中发现有错误,望评论指正,希望大家一起学习,一起进步。
学习C++编译环境:Linux

第六十一课 智能指针类模板

1.智能指针

智能指针的意义
现代C++开发库中最重要的类模板之一
C++中自动内存管理的主要手段
能够在很大程度上避开内存相关的问题
STL中的智能指针auto_ptr
生命周期结束时,销毁所指的内存空间
不能指向堆数组,只能指向堆对象(变量)
一片堆空间只属于一个智能指针对象
多个智能指针对象不能指向同一片堆空间

61-1 auto_ptr使用初探

#include 
#include
#include
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 pt(new Test("ylc"));
    pt->print();
    cout << endl;
    auto_ptr pt1(pt);
    cout << "pt = " << pt.get() << endl;
    cout << "pt1 = " << pt1.get() << endl;
    pt1->print();
    return 0;
}
运行结果
Hello, ylc.
I'm ylc.
pt = 0
pt1 = 0x83ac20
I'm ylc.
Goodbye, ylc.

STL中的其他智能指针
shared_ptr:带有引用计数机制,支持多个指针对象指向同一片内存
weak_ptr:配合weak_ptr而引入的一种智能指针
unique_ptr:一个指针对象指向一片内存空间,不能拷贝构造和赋值(不能所有权的转移)
QT中的智能指针:
QPointer
当其指向的对象被销毁时,它会被自动置空
析构时不会自动销毁所指向的对象
QSharedPointer
引用计数型智能指针
可以被自由地拷贝和赋值
当引用计数为0时才删除指向的对象

61-2 Qt中的智能指针

#include 
#include 
#include 
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 pt(new Test("D.T.Software"));
    QPointer pt1(pt);
    QPointer pt2(pt);
    pt->print();
    pt1->print();
    pt2->print();
    delete pt;
    qDebug() << "pt = " << pt;
    qDebug() << "pt1 = " << pt1;
    qDebug() << "pt2 = " << pt2;
    qDebug() << endl;
    QSharedPointer spt(new Test("Delphi Tang"));
    QSharedPointer spt1(spt);
    QSharedPointer spt2(spt);
    spt->print();
    spt1->print();
    spt2->print();
    return 0;
}

Qt中的其他智能指针
QWeakPointer
QScopedPointer
QScopedArrayPointer
QSharedDataPointer
QExplicitlySharedDataPointer

SmartPointer.h 创建智能指针类模板
#ifndef _SMARTPOINTER_H_
#define _SMARTPOINTER_H_
template
< typename T >
class SmartPointer
{
    T* mp;
public:
    SmartPointer(T* p = NULL)
    {
        mp = p;
    }
    
    SmartPointer(const SmartPointer& obj)
    {
        mp = obj.mp;
        const_cast&>(obj).mp = NULL;
    }
    
    SmartPointer& operator = (const SmartPointer& obj)
    {
        if( this != &obj )
        {
            delete mp;
            mp = obj.mp;
            const_cast&>(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
61-3.cpp
#include 
#include 
#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 pt(new Test("D.T.Software"));
    
    cout << "pt = " << pt.get() << endl;
    
    pt->print();
    
    cout << endl;
    
    SmartPointer pt1(pt);
    
    cout << "pt = " << pt.get() << endl;
    cout << "pt1 = " << pt1.get() << endl;
    
    pt1->print();
    
    return 0;
}
运行结果
Hello, D.T.Software.
pt = 0xf31c20
I'm D.T.Software.
pt = 0
pt1 = 0xf31c20
I'm D.T.Software.
Goodbye, D.T.Software.

小结
智能指针C++中自动内存管理的主要手段
智能指针在各种平台上都有不同的表现形式
智能指针能够尽可能的避开内存相关的问题
STL和Qt中都提供了对智能指针的支持

你可能感兴趣的:(C++深度剖析,C++深度剖析)