c语言中的stl模板指针,【C++】 61_智能指针类模板

智能指针

智能指针的意义

现代 C++ 开发库中最重要的类模板之一

C++ 中内存管理的主要手段

能够在很大程度上避开内存相关的问题

内存泄漏

内存多次释放

STL 中的智能指针 auto_ptr

生命周期结束时,销毁指向的内存(防止内存泄漏)

不能指向堆数组,只能指向堆对象(变量)

一片堆空间只属于一个智能指针对象(防止内存多次释放)

多个智能指针对象不能指向同一片堆空间

编程实验: auto_ptr 使用初探

#include

#include

using namespace std;

class Test

{

private:

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("D.T.Software"));

cout << "pt = " << pt.get() << endl;

pt->print();

cout << endl;

auto_ptr pt1(pt); // 所有权转移 : pt ==> pt1

cout << "pt = " << pt.get() << endl;

cout << "pt = " << pt1.get() << endl;

pt1->print();

return 0;

}

输出:

Hello, D.T.Software

pt = 0x90e7008

I'm D.T.Software

pt = 0

pt = 0x90e7008

I'm D.T.Software

Goodbye, D.T.Software

STL 中的其它指针

share_ptr

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

weak_ptr

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

unique_ptr

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

Qt 中的智能指针

QPointer

当其指向的对象被销毁时,他会被自动置空(防止内存多次释放)

析构时不会自动销毁所指向的对象

QSharedPointer

引用计数型智能指针

可以被自由的拷贝和赋值

当引用计数为 0 时才删除指向的对象(防止内存泄漏)

编程实验: Qt中的智能指针

#include

#include

#include

#include

class Test : public QObject

{

private:

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();

QSharedPointer spt(new Test("D.T.Software"));

QSharedPointer spt1(spt);

QSharedPointer spt2(spt);

spt->print();

spt1->print();

spt2->print();

return 0;

}

输出:

Hello, D.T.Software

I'm "D.T.Software"

I'm "D.T.Software"

I'm "D.T.Software"

Goodbye, "D.T.Software"

pt = QObject(0x0)

pt1 = QObject(0x0)

pt2 = QObject(0x0)

Hello, D.T.Software

I'm "D.T.Software"

I'm "D.T.Software"

I'm "D.T.Software"

Goodbye, "D.T.Software"

Qt 中的其它智能指针

QWeakPointer

QScopedPointer

QScopedArrayPointer

QSharedDataPointer

QExplicitlySharedDataPointer

编程实验: 创建智能指针类模板

SmartPointer.h

#ifndef _SMARTPOINTER_H_

#define _SMARTPOINTER_H_

template

< typename T >

class SmartPoniter

{

private:

T* m_pointer;

public:

SmartPoniter(T* p = NULL);

SmartPoniter(const SmartPoniter& obj);

SmartPoniter& operator = (const SmartPoniter& obj);

T* operator -> ();

T& operator * ();

bool isNull();

T* get();

~SmartPoniter();

};

template

< typename T >

SmartPoniter::SmartPoniter(T* p)

{

m_pointer = p;

}

template

< typename T >

SmartPoniter::SmartPoniter(const SmartPoniter& obj)

{

m_pointer = obj.m_pointer; // 所有权转接

const_cast&>(obj).m_pointer = NULL;

}

template

< typename T >

SmartPoniter& SmartPoniter::operator = (const SmartPoniter& obj)

{

if( this != &obj )

{

delete m_pointer; // 所有权转接

m_pointer = obj.m_pointer;

const_cast&>(obj).m_pointer = NULL;

}

return *this;

}

template

< typename T >

T* SmartPoniter::operator -> ()

{

return m_pointer;

}

template

< typename T >

T& SmartPoniter::operator * ()

{

return *m_pointer;

}

template

< typename T >

bool SmartPoniter::isNull()

{

return (m_pointer == NULL);

}

template

< typename T >

T* SmartPoniter::get()

{

return m_pointer;

}

template

< typename T >

SmartPoniter::~SmartPoniter()

{

delete m_pointer;

}

#endif

main.cpp

#include

#include

#include "SmartPointer.h"

using namespace std;

class Test

{

private:

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 << "Goofbye, " << m_name << endl;

}

};

int main()

{

SmartPoniter pt(new Test("D.T.Software"));

cout << "pt = " << pt.get() << endl;

pt->print();

cout << endl;

SmartPoniter pt1(pt);

cout << "pt = " << pt.get() << endl;

cout << "pt = " << pt1.get() << endl;

pt1->print();

return 0;

}

输出:

Hello, D.T.Software

pt = 0x815e008

I'm D.T.Software

pt = 0

pt = 0x815e008

I'm D.T.Software

Goofbye, D.T.Software

小结

智能指针是 C++ 中自动内存管理的主要手段

智能指针在各种平台上都有不同的表现形式

智能指针能够尽可能的避开内存相关的问题

STL 和 Qt 中都提供了对智能指针的支持

以上内容参考狄泰软件学院系列课程,请大家保护原创

你可能感兴趣的:(c语言中的stl模板指针)