数组类模板

  • 智能指针的意义
  1. 现代C++开发库中最重要的类模板之一
  2. C++中自动内存管理的主要手段
  3. 能够在很大程度上避开内存相关的问题(内存泄漏,指针,多次释放)
  • STL中的智能指针(auto_ptr)
  1. 生命周期结束时,销毁指向的内存空间
  2. 不能指向堆数组,只能指向堆对象(变量)
  3. 一片堆空间只属于一个智能指针对象
  4. 多个智能指针对象不能指向同一片堆空间
  • 范例程序
#include <iostream>
#include <string>
#include <memory>
using namespace std;
 
class Test
{
    string name;
public:
    Test(string str)
    {
        cout << "Hello "<< str << endl;
        name = str;
    }
    ~Test()
    {
        cout << "GoodBye " << name << endl;
    }
    void print()
    {
        cout << "I'm "<<name<<endl;
    }
};
 
int main()
{
    //可以看成这个样子:Test*pt = new Test("chenge");
    //使用智能指针,内存分配的内存空间的所有权只归一个auto_ptr对象所有。
    auto_ptr<Test> pt(new Test("chenge"));
    pt->print();
    cout<<"pt = "<<pt.get()<<endl;
    //可以看成这个样子:Test*pt1 = pt;
    //pt1和pt指向同一块内存,如果连续释放两次同一块内存,程序会崩溃
    //在程序运行到这儿,内存分配的空间的所有权就发生了转移,归pt1所有。
    //这也是体现了智能指针的智能所在,能够防止同一块内存多次释放,内存泄漏等问题
    auto_ptr<Test> pt1(pt);
    pt1->print();
    cout<<"pt = "<<pt.get()<<endl;
    cout<<"pt1 = "<<pt1.get()<<endl;
    return 0;
}
  • 运行结果
  • STL中的其他智能指针
  1. shared_ptr:带有引用计数机制,支持多个指针对象指向同一片内存
  2. weak_ptr:配合shared_ptr而引入的一种智能指针
  3. unique_ptr:一个指针对象指向一片内存空间,不能拷贝构造和赋值。
  • Qt中的智能指针
  1. QPointer:当其指向的对象销毁时,它会被自动置空,析构时不会自动销毁所指向的对象
  2. QSharedPointer:引用计数型智能指针,可以自由的拷贝和赋值,当引用计数为0时才删除指向的对象。
  3. 其它智能指针:QWeakPointer,QScopedPointer,QScopedArrayPointer,QSharedDataPointer,QExplicitlySharedDataPointer。
  • 智能指针模板范例程序
Smart_Pointer.h
#ifndef SMART_POINTER_H
#define SMART_POINTER_H
template<typename T>
class Smart_Pointer
{
    T* mp;
public:
    Smart_Pointer(T* p = NULL);
    //拷贝构造函数,执行深拷贝(初始化的情况)
    Smart_Pointer(const Smart_Pointer<T>& obj);
    //重载赋值操作符,实现深拷贝(赋值情况)
    Smart_Pointer& operator = (const Smart_Pointer<T>& obj);
    T* operator ->();
    T& operator *();
    ~Smart_Pointer();
    //如果mp等于NULL,返回true:1
    bool isNULL();
    T* get();
};
template<typename T>
Smart_Pointer<T>::Smart_Pointer(T* p)
{
    mp = p;
}
template<typename T>
Smart_Pointer<T>::Smart_Pointer(const Smart_Pointer<T>& obj)
{
    mp = obj.mp;
    //剥夺初始化对象的只读属性
    const_cast<Smart_Pointer<T>&>(obj).mp = NULL;
}
template<typename T>
Smart_Pointer<T>& Smart_Pointer<T>:: operator = (const Smart_Pointer<T>& obj)
{
    //重载赋值操作符前,需要先判断一下是否是自赋值操作
    if (this != &obj)
    {
        delete mp;
        mp = obj.mp;
        const_cast<Smart_Pointer<T>&>(obj).mp = NULL;
    }
    return *this;
}
template<typename T>
T* Smart_Pointer<T>:: operator ->()
{
    //返回的是个类型的地址,所以要重载->操作符
    return mp;
}
template<typename T>
T& Smart_Pointer<T>::operator *()
{
    //要返回这个值得本身
    return *mp;
}
template<typename T>
Smart_Pointer<T>::~Smart_Pointer()
{
    delete mp;
}
template<typename T>
//如果mp等于NULL,返回true:1
bool Smart_Pointer<T>::isNULL()
{
    return (mp == NULL);
}
template<typename T>
T* Smart_Pointer<T>::get()
{
    return mp;
}
#endif // SMART_POINTER_H
main.cpp
#include <iostream>
#include <string>
#include "smart_pointer.h"
using namespace std;
class Test
{
    string name;
public:
    Test(string str)
    {
        cout << "Hello " << str << endl;
        name = str;
    }
    ~Test()
    {
        cout << "GoodBye " << name << endl;
    }
    void print()
    {
        cout << "I'm " << name << endl;
    }
};
int main()
{
    Smart_Pointer<Test> pt(new Test("chenge"));
    pt->print();
    cout << "pt = " << pt.get() << endl;
    Smart_Pointer<Test> pt1;
    pt1->print();
    cout << "pt = " << pt.get() << endl;
    cout << "pt1 = " << pt1.get() << endl;
    return 0;
}
  • 小结
  1. 智能指针C++中自动内存管理的主要手段
  2. 智能指针在各种平台都有不同的表现
  3. 智能指针能够尽可能的避开内存相关的问题
  4. STL和QT中都提供了对智能指针的支持
 
 
 
 
 
posted @ 2020-02-09 17:22  认真做个普通人  阅读(283)  评论(0编辑  收藏  举报