你能解释什么是智能指针的类型,它们如何工作以及何时使用它们?
此外,在其他人写的接口中接收或传递原始指针时,“协议”是什么?
谢谢.
解决方法
Can you explain what are the types of smart pointers,how do they work and when to use them?
有一些他们.简而言之:
scoped_ptr
<boost/scoped_ptr.hpp>
Simple sole ownership of single
objects. Noncopyable.
scoped_array
<boost/scoped_array.hpp>
Simple sole
ownership of arrays. Noncopyable.
shared_ptr
<boost/shared_ptr.hpp>
Object ownership shared among
multiple pointers.
shared_array
<boost/shared_array.hpp>
Array
ownership shared among multiple
pointers.
weak_ptr
<boost/weak_ptr.hpp>
Non-owning
observers of an object owned by
shared_ptr.
intrusive_ptr
<boost/intrusive_ptr.hpp>
Shared
ownership of objects with an embedded
reference count.
(这是从Boost文档,并注意到它们也有这样的指针的容器!)
Also,what is the “protocol” when receiving or passing raw pointers in interfaces written by other people?
对我而言,最重要的规则是:
>宪章资格>不要释放我没有分配的东西检查所有权/移动语义的转移