目录
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;
}