Switching form C# to C++ out of curiosity.
What else did I miss out on that makes programming in C++ less of a nut job?
Switching form C# to C++ out of curiosity.
What else did I miss out on that makes programming in C++ less of a nut job?
Other urls found in this thread:
en.cppreference.com
twitter.com
Stay there, we're full.
std::unique_ptr
elaborate
C++ dev's uppity stereotype never ceases to amaze
>using namespace std
Unique pointer is supposed to express exclusive ownership, so he's creating an exclusive pointer to a shared pointer, which is just trying to be funny I guess.
What happens to the underlying object in that case? Can it only be passed by value?
>What happens to the underlying object in that case?
Nothing, it's a pointer to a pointer to the object, not a pointer to the object.
unique_ptr wraps a pointer to the object and ensures automatic destruction (i.e. RAII).
It is "unique" because the copy constructor and copy assignment operators are deleted,. Hence you can't pass unique_ptr by value. You can access and pass the raw pointer to the object via ->get(). This can be passed around freely.
If you intend to change ownership, unique_ptr can be moved as it implements move constructor and move assignment constructor. You can think of these as functions that will be selected at overload resolution instead of the copy constructor/assignment operator. Only move a unique_ptr if you intend to change ownership. If you only want to use the underlying object in functions, use raw_ptr.
A shared_ptr can be passed by value. Any time it is copied, its reference count will increment, and likewise decrement upon destruction. When the reference count reaches zero, the underlying object will be destroyed.
You can wrap a shared_ptr in a unique_ptr, but it is pointless.