Smart Pointers #15
armingli
announced in
Programming
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
std::unique_ptr
is a smart pointer with unique ownership semantics.unique_ptr
represents unique ownership, it cannot be copied . Usingstd::move()
to explicitly move ownership.You can offer custom deleter. It is useful to manage other resources instead of just memory.
std::shared_ptr
is a smart pointer with shared ownership semantics using reference counting .std::weak_ptr
, contains a reference to a resource managed by ashared_ptr
.weak_ptr
does not own the resource, so theshared_ptr
is not prevented from deallocating the resource.weak_ptr
does not destroy the pointed-to resource when the it is destroyed.weak_ptr
required ashared_ptr
or anotherweak_ptr
as argument.weak_ptr
, you need to convert it to ashared_ptr
:lock()
method on aweak_ptr
instance, which returns ashared_ptr
. The returnedshared_ptr
isnullptr
if theshared_ptr
associated with theweak_ptr
has been deallocated.shared_ptr
instance and give aweak_ptr
as argument to theshared_ptr
constructor.Beta Was this translation helpful? Give feedback.
All reactions