1 智能指针介绍
1.1 智能指针的意义
智能指针具有如下意义:
- 现代C++开发库中最重要的类模板之一。
- C++中自动内存管理的主要手段。
- 能够很大程度上避开内存相关的问题。
1.2 STL中的智能指针
STL中的智能指针auto_ptr:
- 生命周期结束时,销毁指向的内存空间。
- 不能指向堆数组,只能指向堆对象(变量)。
- 一片堆空间只属于一个智能指针对象。
- 多个智能指针对象不能指向同一片堆空间。
编程实验: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);
cout << "pt = " << pt.get() << endl;
cout << "pt1 = " << pt1.get() << endl;
pt1->print();
return 0;
}
STL中的其它智能指针:
- shared_ptr:带有引用计数机制,支持多个指针对象指向同一片内存。
- weak_ptr:配合shared_ptr而引入的一种智能指针。
- unique_ptr:一个指针对象指向一片内存空间,不能拷贝构造和赋值。
1.3 Qt中的智能指针
QPointer:
- 当其指向的对象被销毁时,它会被自动置空。
- 析构时不会自动销毁所指向的对象。
QSharedPointer:
- 引用计数型智能指针。
- 可以被自由的拷贝和赋值。
- 当引用计数为0时才删除指向的对象。
编程实验: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中的其它智能指针:
2 创建智能指针类模板
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) // 类内实现不需要加<T>
{
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
main.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;
}
参考资料:
来源:CSDN
作者:SlowIsFastLemon
链接:https://blog.csdn.net/SlowIsFastLemon/article/details/104303090