C++ unique_ptr and shared_ptr

文章讨论了C++中std::dynamic_pointer_cast在shared_ptr和unique_ptr之间的使用,强调了unique_ptr需要手动处理rawpointer并释放基指针,同时指出移动赋值不会增加引用计数,而dynamic_cast会因为返回新的shared_ptr而增加。
摘要由CSDN通过智能技术生成
  1. You can only call std::dynamic_pointer_cast on shared_ptr. For unique_ptr, you need to dynamic_cast its raw pointer and create a new unique_ptr with it and manully release the base pointer if the cast succeeds.
  2. std::move won’t temporarily increase refcount, but dynamic_pointer_cast will since it returns a new shared_pointer.
  3. refcount is a pointer in shared_ptr.
  4. shared_ptr of different types can share the same object (i.e. pointers pointing to the same obj, and refcount also points to the same one). Especially when you create shared_ptr with dynamic_cast
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值