1)std::unique_ptr(独占所有权)
特点:
独占资源的所有权,同一时间只能有一个 unique_ptr 指向特定对象。
不可复制,但可以通过 std::move 转移所有权。
轻量级,几乎无额外开销(与裸指针性能接近)。
2)std::shared_ptr(共享所有权)
特点:
通过引用计数实现多个指针共享同一资源。
当最后一个 shared_ptr 被销毁时,资源自动释放。
支持自定义删除器(如文件句柄、网络连接等)。
3)std::weak_ptr(弱引用)
特点:
不拥有资源的所有权,不增加引用计数。
需通过 lock() 方法转换为 shared_ptr 以访问资源(防止访问已释放对象)。
测试用例
weak_ptr解决循环引用和lock获取shared_ptr
class B; // 前向声明
class A
{
public:
std::shared_ptr<B> b_ptr;
A() { std::cout << "A 构造" << std::endl; }
~A() { std::cout << "A 析构" << std::endl; }
void who(){ std::cout << "I'm A " << std::endl; }
};
class B
{
public:
std::weak_ptr<A> a_ptr; // 使用 weak_ptr 替代 shared_ptr
B() { std::cout << "B 构造" << std::endl; }
~B() { std::cout << "B 析构" << std::endl; }
void who(){ std::cout << "I'm B " << std::endl; }
};
int main(int argc, char *argv[])
{
auto a = std::make_shared<A>();
auto b = std::make_shared<B>();
a->b_ptr = b;
b->a_ptr = a; // 不会增加引用计数
if (auto spt = b->a_ptr.lock())
{ // 转换为 shared_ptr
spt->who();
}
else
{
std::cout << "对象已被释放" << std::endl;
}
// 退出作用域时,a 和 b 都能正常析构
return 0;
}
shared_ptr和unique_ptr的初始化
class Test
{
public:
Test(int time = 0) : time_(time)
{
std::cout << "Test: " << time_ << std::endl;
}
~Test()
{
std::cout << "~Test: " << time_ << std::endl;
}
int time_;
};
int main(int argc, char *argv[])
{
std::shared_ptr<Test> p0 = std::make_shared<Test>(1);
std::unique_ptr<Test> p1;
p1.reset(new Test(-1));
std::unique_ptr<Test> p2(new Test);
//p1 = p2;//error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Test; _Dp = std::default_delete<Test>]’
return 0;
}