- 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.
- std::move won’t temporarily increase refcount, but dynamic_pointer_cast will since it returns a new shared_pointer.
- refcount is a pointer in shared_ptr.
- 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
C++ unique_ptr and shared_ptr
最新推荐文章于 2024-10-05 21:10:28 发布
文章讨论了C++中std::dynamic_pointer_cast在shared_ptr和unique_ptr之间的使用,强调了unique_ptr需要手动处理rawpointer并释放基指针,同时指出移动赋值不会增加引用计数,而dynamic_cast会因为返回新的shared_ptr而增加。
摘要由CSDN通过智能技术生成