cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Deleting a unique_ptr
Deleting a unique_ptr
Dec 14, 2014 at 2:41am UTC
UglyIgloo
(28)
I have a unique_ptr that is stored in multiple vectors, and I don't want to manually take it out of scope by removing it from every vector.
Would deleting the object that it POINTS to take the unique_ptr out of scope?
Dec 14, 2014 at 3:19am UTC
JLBorges
(13770)
> I have a unique_ptr that is stored in multiple vectors
The semantics of
std::unique_ptr
is sole ownership of an object.
Do not, repeat do not, create more than one
std::unique_ptr
pointing to the same object.
For shared ownership semantics, use
std::shared_ptr
http://en.cppreference.com/w/cpp/memory/shared_ptr
In either case, let the smart pointer manage object life-time.
In an extreme case, if you are compelled to manually delete an object whose ownership is managed via smart pointers, delete it after asking the smart pointer(s) to release ownership.
http://en.cppreference.com/w/cpp/memory/shared_ptr/reset
http://en.cppreference.com/w/cpp/memory/unique_ptr/release
Topic archived. No new replies allowed.