CPlusPlus

C++11智能指针

2019-03-22  本文已影响0人  别念_

智能指针 unique_ptr shared_ptr weak_ptr


在C98中,智能指针通过一个模板类型"auto_ptr"来实现。auto_ptr以对象的方式管理分配的内存,并在适当的事件(比如析构),放释所获得的堆内存。这种对内存管理的方式只需要程序员将new操作返回的指针作为auto_ptr的初始值即可,程序员不用再显示地调用delete,比如:

  auto_ptr(new int);

不过auto_ptr也有一些缺点(拷贝时返回一个左值,不能调用delete[]等);


#include<iostream>
#include<memory>
using namespace std;

int main()
{
    shared_ptr<int> sp1(new int(22));
    shared_ptr<int> sp2 = sp1;

    cout << *sp1 << endl;               //22
    cout << *sp2 << endl;               //22

    sp1.reset();
    cout << *sp2 << endl;               //22
        return 0;
}

#include<iostream>
#include<memory>
using namespace std;

int main()
{
    unique_ptr<int> up1(new int(11));   //无法复制的unique_ptr
    //unique_ptr<int> up2 = up1;        //不能通过编译
    cout << *up1 << endl;               //11


    unique_ptr<int> up3 = move(up1);    //转移up1的内存所有权到up3
    cout << *up3 << endl;
    //cout << *up1 << endl;             //运行出错

    up3.reset();                        //显示释放内存
    up1.reset();                        //不会导致运行时错误
    cout << *up3 << endl;               //运行错误

  return 0;
}

#include<iostream>
#include<memory>
using namespace std;

void Check(weak_ptr<int> &wp)
{
    shared_ptr<int> sp = wp.lock();             //转换为shared_ptr<int>
    if (sp != nullptr)
        cout << "still" << *sp << endl;
    else
        cout << "pointer is invalid" << endl;
}

int main()
{

    shared_ptr<int> sp1(new int(22));
    shared_ptr<int> sp2 = sp1;

    weak_ptr<int> wp = sp1;             //指向shared_ptr所指对象

    cout << *sp1 << endl;               //22
    cout << *sp2 << endl;               //22
    Check(wp);                          //still 22

    sp1.reset();
    cout << *sp2 << endl;               //22
    Check(wp);                          //still 22

    sp2.reset();
    Check(wp);                          //pointer is invalid
    return 0;
}

上一篇 下一篇

猜你喜欢

热点阅读