C++智能指针之 auto_ptr和unique_ptr

23 篇文章 17 订阅

auto_ptr是用于C++11之前的智能指针。由于 auto_ptr 基于排他所有权模式:两个指针不能指向同一个资源,复制或赋值都会改变资源的所有权。

auto_ptr 主要有两大问题:

1、复制和赋值会改变资源的所有权,不符合人的直觉。
2、在 STL 容器中无法使用auto_ptr ,因为容器内的元素必需支持可复制(copy constructable)和可赋值(assignable)。

unique_ptr特性
1、拥有它所指向的对象
2、 无法进行复制构造,也无法进行复制赋值操作
3、保存指向某个对象的指针,当它本身离开作用域时会自动释放它指向的对象。

unique_ptr可以:
o 为动态申请的内存提供异常安全
o 将动态申请内存的所有权传递给某个函数
o 从某个函数返回动态申请内存的所有权
o 在容器中保存指针
unique_ptr十分依赖于右值引用和移动语义。

在C++11中已经放弃auto_ptr转而推荐使用unique_ptr和shared_ptr。unique跟auto_ptr类似同样只能有一个智能指针对象指向某块内存.但它还有些其他特性。unique_ptr对auto_ptr的改进如下:1、auto_ptr支持拷贝构造与赋值操作,但unique_ptr不直接支持
auto_ptr通过拷贝构造或者operator=赋值后,对象所有权转移到新的auto_ptr中去了,原来的auto_ptr对象就不再有效,这点不符合人的直觉。unique_ptr则直接禁止了拷贝构造与赋值操作。

auto_ptr<int> ap(new int(10));
auto_ptr<int> one (ap) ; // ok
auto_ptr<int> two = one; //ok

unique_ptr<int> ap(new int(10));
unique_ptr<int> one (ap) ; // 会出错
unique_ptr<int> two = one; //会出错

2,unique_ptr可以用在函数返回值中
unique_ptr像上面这样一般意义上的复制构造和赋值或出错,但在函数中作为返回值却可以用.

unique_ptr<int> GetVal( ){
unique_ptr<int> up(new int(10 );
return up;
}
unique_ptr<int> uPtr = GetVal(); //ok

实际上上面的的操作有点类似于如下操作

unique_ptr<int> up(new int(10);
unique_ptr<int> uPtr2 = std:move( up) ;//这里是显式的所有权转移. 把up所指的内存转给uPtr2了,而up不再拥有该内存

3,unique_ptr可做为容器元素
我们知道auto_ptr不可做为容器元素,会导致编译错误。虽然unique_ptr同样不能直接做为容器元素,但可以通过move语意实现。

unique_ptr<int> sp(new int(88) );
vector<unique_ptr<int> > vec;
vec.push_back(std::move(sp));
// vec.push_back( sp ); error:
// cout << *sp << endl;

std::move让调用者明确知道拷贝构造、赋值后会导致之前的unique_ptr失效。

#include <memory>
#include <vector>
#include <cassert>
using namespace std;

unique_ptr<int> GetVal( ){
unique_ptr<int> up(new int(10));
return up;
}

int main()
{
    // auto_ptr支持拷贝构造与赋值,拷贝构造或赋值后对象所有权转移
    auto_ptr<int> aPtr(new int(10));
    auto_ptr<int> a1 (aPtr);  // call copy constuctor, OK
    assert(aPtr.get() == nullptr && a1.get() != nullptr);

    auto_ptr<int> a2(NULL);
    a2 = a1;        // call operator=, OK
    assert(a1.get() == nullptr && a2.get() != nullptr);

    // unique_ptr不支持直接拷贝构造与赋值
    unique_ptr<int> uPtr(new int(10));
    // unique_ptr<int> u1 (uPtr);       // call copy constructor, error 
    // Error: Call to implicitly-deleted copy constructor of 'unique_ptr<int>'

    // unique_ptr<int> u2 = nullptr;
    // u2 = uPtr;               // call operator=, error
    // Error: Overload resolution selected implicitly-deleted copy assignment operator

    // unique_ptr支持move语意的拷贝构造
    unique_ptr<int> u1(std::move(uPtr));
    assert(uPtr == nullptr && u1 != nullptr);

    // unique_ptr支持move语意的赋值
    unique_ptr<int> u2 = std::move(u1) ; // 把u1所指的内存转给u2了,而u1不再拥有该内存
    assert(u1 == nullptr && u2 != nullptr);

    // unique_ptr通过move语意可放入STL容器
    unique_ptr<int> sp(new int(100));
    vector<unique_ptr<int>> vec;
    vec.push_back(std::move(sp));

    // vec.push_back(sp);
    // Error: Call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<int, std::__1::default_delete<int> >'

    assert(sp == nullptr);
    // cout << *sp << endl;  // crash at this point, sp is nullptr now

    unique_ptr<int> g_uPtr = GetVal();   //ok
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值