一. 指针存在的问题
智能指针引入为了解决的问题:
内存泄漏
- 动态申请堆空间,用完后不归还
- C++语言中没有垃圾回收的机制
- 指针无法控制所指堆空间的生命周期
示例:
#include <iostream>
#include <string>
using namespace std;
class Test
{
int i;
public:
Test(int i)
{
this->i = i;
}
int value()
{
return i;
}
~Test()
{
}
};
int main()
{
for(int i=0; i<5; i++)
{
Test* p = new Test(i);//此处造成内存泄漏
cout << p->value() << endl;
//用完指针p需要归还
}
return 0;
}
二. 智能指针的功能设想
- 需要一个特殊的指针
- 指针生命周期结束时主动释放堆空间
- 一片堆空间最多只能由一个指针标识
- 杜绝指针运算和指针比较
三. 解决方案
- 重载指针特征操作符(-> 和 *)
- 只能通过类的成员函数重载
- 重载函数不能使用参数
- 只能定义一个重载函数
智能指针示例:
#include <iostream>
#include <string>
using namespace std;
class Test
{
int i;
public:
Test(int i)
{
cout << "Test(int i)" << endl;
this->i = i;
}
int value()
{
return i;
}
~Test()
{
cout << "~Test()" << endl;
}
};
class Pointer //智能指针类
{
Test* mp; //指向Test的数据成员指针
public:
Pointer(Test* p = NULL) //可以被堆空间上的内存地址初始化
{
mp = p;
}
//重载赋值操作符和拷贝构造函数,解决“一片堆空间最多只能由一个指针标识”
Pointer(const Pointer& obj)
{
//delete mp; //此处存在则存在野指针,报错
mp = obj.mp;
//obj.mp = NULL; //进行所有权的传递
const_cast<Pointer&>(obj).mp = NULL;//去掉obj的只读属性
}
Pointer& operator = (const Pointer& obj)
{
if(this != &obj ) //没有自赋值
{
delete mp;
mp = obj.mp;
const_cast<Pointer&>(obj).mp = NULL;
}
return *this;
}
Test* operator -> ()
{
return mp;
}
Test& operator * () //返回当前指针所指向的变量或者对象
{
return *mp;
}
bool isNull()
{
return (mp == NULL);
}
~Pointer()
{
delete mp;
}
};
int main()
{
Pointer p1 = new Test(0); //通过类对象模拟指针的行为
cout << p1->value() << endl;
Pointer p2 = p1; //拷贝构造的时候,不能先删除,还是野指针
cout << p1.isNull() << endl;
cout << p2->value() << endl;
return 0;
}
运行结果
Test(int i)
0
1
0
~Test()
智能指针的使用军规:
只能用来指向堆空间中的对象或者变量
小结:
指针特征操作符(-> 和 *)
重载指针特征符能够使用对象代替指针
智能指针只能用于指向堆空间中的内存
智能指针的意义在于最大程度的避免内存问题
四.智能指针类模板
1.智能指针的意义
- 现代C++开发库中最重要的类模板之一
- C++中自动内存管理的主要手段
- 能够在很大程度上避开内存相关的问题
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("superman")); //一片堆空间只属于一个智能指针对象pt
cout << "pt = " << pt.get() << endl;
pt->print();
cout << endl;
auto_ptr<Test> pt1(pt); //使用对象pt来初始化pt1, pt将对象的所有权转给了pt1,不再管理堆空间上的对象
cout << "pt = " << pt.get() << endl; //返回堆空间的起始地址
cout << "pt1 = " << pt1.get() << endl;
pt1->print();
return 0;
}
运行结果
Hello, superman.
pt = 0x863e008
I'm superman.
pt = 0
pt1 = 0x863e008 //堆空间的起始地址转移
I'm superman.
Goodbye, superman.
STL中的其他智能指针:
- shared_ptr
带有引用计数机制,支持多个指针对象指向同一片内存 - weak_ptr
配合shared_ptr而引入的一种智能指针 - unique_ptr
一个指针对象指向一片内存空间,不能拷贝构造和赋值
2.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("xx"));
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("superman")); //引用计数为3
QSharedPointer<Test> spt1(spt); //三个智能指针指向了同一堆空间中的对象
QSharedPointer<Test> spt2(spt);
spt->print();
spt1->print();
spt2->print();
return 0;
}
编译结果:
Hello xx
I'm "xx". //没有调用析构函数, pt结束没有销毁堆空间里面指向的对象
I'm "xx".
I'm "xx".
Goodbye, "xx". //手工delete释放
pt = QObject(0x0)
pt1 = QObject(0x0)
pt2 = QObject(0x0) //QPointer类模板的对象所指向的堆空间被释放了,所有指向堆空间的智能指针都被置空,避免内存多次释放
Hello superman
I'm "superman".
I'm "superman".
I'm "superman".
Goodbye, "superman".
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
SmartPointer.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("superman"));
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;
}
运行结果: //类似于auto_ptr的功能
Hello, superman.
pt = 0xf31c20
I'm superman.
pt = 0
pt1 = 0xf31c20
I'm superman.
Goodbye, superman.
小结:
智能指针是C++中自动内存管理的主要手段
智能指针在各种平台上都有不同的表现形式
智能指针能够尽可能的避开内存相关的问题
STL和Qt中都提供了对智能指针的支持