From the course: Modern C++: Advanced Techniques and Features
Unlock this course with a free trial
Join today to access over 24,700 courses taught by industry experts.
Weak pointers - C++ Tutorial
From the course: Modern C++: Advanced Techniques and Features
Weak pointers
- In this section, we're going to look at weak pointer. Weak pointer is quite similar to shared pointer. You create a weak pointer from a shared pointer, except that weak pointer doesn't participate in the reference counting. So once all the shared pointers have disappeared, the weak pointer isn't sufficient to keep the object alive. Hence the name. It's a weak pointer. So we're going to look at the syntax for creating a weak pointer, quite straightforward, and then we'll see how to use it and we'll talk about why you'd use it as well. Okay, so stud weak pointer. It's a smart pointer used in conjunction with the shared pointer, but it doesn't participate in reference counting. So first of all, you'd have to create a shared pointer to an object. So you'd have created a dynamic object, probably a bank account, and then you'd have a shared pointer that would point to it. And maybe you've got a couple of shared pointers. Maybe you've gone like this. So you've got two shared pointers, and…