How and why to use unique_ptr.
What exactly makes a "smart" pointer smart? The term smart pointer in C++ has come to refer to a pointer-like wrapper class that manages the lifetime of a pointed-to object, ensuring that the object is deleted exactly once without requiring the programmer to remember to do anything special. The most popular examples are std::unique_ptr and std::shared_ptr, which model unique ownership and shared ownership respectively. In this video we break down unique_ptr, which is by far the simpler of the two. We even implement the main ideas of unique_ptr in under 50 lines of code. Using unique_ptr can drastically improve the robustness of your code, usually at zero runtime cost, which is why you should prefer to use unique_ptr unless you have a good reason not to.
― mCoding with James Murphy (https://mcoding.io)
Source code: https://github.com/mCodingLLC/VideosS...
unique_ptr docs: https://en.cppreference.com/w/cpp/mem...
SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!
/ mcoding
Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)
https://www.paypal.com/donate/?hosted...
Want to donate crypto? Check out the rest of my supported donations on my website!
https://mcoding.io/donate
Top patrons and donors: Dragos C, Laura M, Jameson, Neel R, Vahnekie, Matt R, Johan A, Mark M, Mutual Information, Casey G
BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord: / discord
Github: https://github.com/mCodingLLC/
Reddit: / mcoding
Facebook: / james.mcoding
CHAPTERS
---------------------------------------------------
0:00 Intro
0:45 Raw pointer example
1:40 Problems with raw pointers
2:46 The idea behind unique_ptr
3:23 Converting example to unique_ptr
5:15 NOT the end
5:58 Writing unique_ptr
6:23 m_ptr and destructor
6:52 Constructors
7:07 unique_ptr is not copyable
7:37 release and reset, exchange
8:17 Move constructor and move assignment operator
9:24 Making it pointer-like
10:00 Zoom out and make_unique
10:14 The benefits
10:49 Not a silver bullet
11:32 Outro