C++筆記 第六十一課 智慧指標類模板---狄泰學院
阿新 • • 發佈:2018-12-01
如果在閱讀過程中發現有錯誤,望評論指正,希望大家一起學習,一起進步。
學習C++編譯環境:Linux
第六十一課 智慧指標類模板
1.智慧指標
智慧指標的意義
現代C++開發庫中最重要的類模板之一
C++中自動記憶體管理的主要手段
能夠在很大程度上避開記憶體相關的問題
STL中的智慧指標auto_ptr
生命週期結束時,銷燬所指的記憶體空間
不能指向堆陣列,只能指向堆物件(變數)
一片堆空間只屬於一個智慧指標物件
多個智慧指標物件不能指向同一片堆空間
61-1 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("ylc")); pt->print(); cout << endl; auto_ptr<Test> 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 <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; }
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<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
61-3.cpp
#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;
}
執行結果
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中都提供了對智慧指標的支援