From the course: C++ Best Practices for Developers

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Which pointer when?

Which pointer when? - C++ Tutorial

From the course: C++ Best Practices for Developers

Start my 1-month free trial

Which pointer when?

- [Instructor] Which pointer, when? Shared pointers have reference counters associated with them. Each time it is copied, its reference count goes up and the count goes down when the pointer goes out of scope. When the count reaches zero, it is deleted automatically. The overhead is higher than other pointer types, but worth it if you need this kind of automatic lifetime management. Unique pointers allow for unique ownership of a resource. If you have it, you own it and you need to take care of it. Copying is not allowed, by definition, but it does have support for move semantics, so you can easily transfer ownership. Weak pointers are one of those things that you rarely need, but when you do, you'll know. I've used them in graphics programming to stop the circular reference between parent and child objects prevent either from being freed. In general, unique pointers should be your default.

Contents