C++智能指针之弱指针(std::weak_ptr)

1 概述

  从C++11开始C++语言越来向现代化语言转变。尤其是智能指针的引入,代码中不会直接使用new/delete了。C++11智能指针有三种分别是:shared_ptr,weak_ptr 和unique_ptr 。

2 弱指针(weak_ptr)

weak_ptr是C++11引入的,用来管理share_ptr,不能单独使用,用来解决shared_ptr的互相引用性.其类图如下:
在这里插入图片描述

weak_ptr的特性:

  • 可复制性,体现在拷贝构造函数和拷贝赋值函数,复制不会增加引用计数
  • 可移动性,体现在移动构造函数和移动赋值函数C++14及之后版本支持。
  • 可交换性,通过swap函数交换两个unique_ptr对象
  • 可重置性,通过rest函数重置管理指针

2 使用实例

void WeakPtrSuite::lock()
{
    std::shared_ptr<int> b(new int(10));
    std::weak_ptr<int> a;
    std::shared_ptr<int> c = a.lock();
    TEST_ASSERT_EQUALS(0, c.use_count())
    a = b;
    c = a.lock();
    TEST_ASSERT_EQUALS(10, *c)
}

weak_ptr通过lock返回管理的shared_ptr

3 接口使用

3.1 construct/use_count

获取指针引用计数

void WeakPtrSuite::use_count()
{
    std::shared_ptr<int> d(new int(10));
    std::weak_ptr<int> a;
    std::weak_ptr<int> b(a);
    std::weak_ptr<int> c(d);

    TEST_ASSERT_EQUALS(0, a.use_count())
    TEST_ASSERT_EQUALS(0, b.use_count())
    TEST_ASSERT_EQUALS(1, c.use_count())
}

3.2 assign

赋值操作

void WeakPtrSuite::assign()
{
    std::weak_ptr<int> a;
    std::weak_ptr<int> b;
    std::weak_ptr<int> c;
    std::shared_ptr<int> d(new int(10));

    TEST_ASSERT_EQUALS(0, a.use_count())
    TEST_ASSERT_EQUALS(0, b.use_count())
    TEST_ASSERT_EQUALS(0, c.use_count())
    b = a;
    c = d;
    TEST_ASSERT_EQUALS(0, b.use_count())
    TEST_ASSERT_EQUALS(1, c.use_count())
    b = c;
    TEST_ASSERT_EQUALS(1, b.use_count())
    TEST_ASSERT_EQUALS(1, c.use_count())
}

3.3 lock

返回管理的shared_ptr

void WeakPtrSuite::lock()
{
    std::shared_ptr<int> b(new int(10));
    std::weak_ptr<int> a;
    std::shared_ptr<int> c = a.lock();
    TEST_ASSERT_EQUALS(0, c.use_count())
    a = b;
    c = a.lock();
    TEST_ASSERT_EQUALS(10, *c)
}

3.3 swap

交换两个指针

void WeakPtrSuite::swap()
{
    std::shared_ptr<int> a(new int(10));
    std::shared_ptr<int> b(new int(20));
    std::weak_ptr<int> c(a);
    std::weak_ptr<int> d(b);
    TEST_ASSERT_EQUALS(10, *c.lock())
    TEST_ASSERT_EQUALS(20, *d.lock())
    c.swap(d);
    TEST_ASSERT_EQUALS(20, *c.lock())
    TEST_ASSERT_EQUALS(10, *d.lock())
}

3.4 reset

重置不再管理的指针

void WeakPtrSuite::reset()
{
    std::shared_ptr<int> b(new int(10));
    std::weak_ptr<int> a(b);
    TEST_ASSERT_EQUALS(1, a.use_count())
    a.reset();
    TEST_ASSERT_EQUALS(0, a.use_count())
}

3.5 expired

管理指针是否过期

void WeakPtrSuite::expired()
{
    std::shared_ptr<int> b(new int(10));
    std::weak_ptr<int> a(b);
    TEST_ASSERT_EQUALS(false, a.expired())
    a.reset();
    TEST_ASSERT_EQUALS(true, a.expired())
}

3.6 owner_before

指针owner小于比较

void WeakPtrSuite::owner_before()
{
    std::shared_ptr<int> a (new int (10));
    std::shared_ptr<int> b (new int (10));
    std::weak_ptr<int> wa (a);
    std::weak_ptr<int> wb (b);
    TEST_ASSERT_EQUALS(true, wa.owner_before(wb))
    TEST_ASSERT_EQUALS(false,  wb.owner_before(wa))

    int * p = new int (10);

    std::shared_ptr<int> c (new int (20));
    std::shared_ptr<int> d (c, p);  // alias constructor
    std::weak_ptr<int> e(d);

    //comparing c and e
    TEST_ASSERT_EQUALS(false, c < e.lock())
    TEST_ASSERT_EQUALS(true, e.lock() < c)
    TEST_ASSERT_EQUALS(false, e.owner_before(c))
    TEST_ASSERT_EQUALS(false, c.owner_before(e))

    delete p;
}
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysnow010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值