std::weak_ptr - A non-owning smart pointer | Modern Cpp Series Ep. 35
Вставка
- Опубліковано 1 жов 2024
- ►Full C++ Series Playlist: • The C++ Programming La...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►Lesson Description: In this lesson I show you how to use a std::weak_ptr. Weak pointers are probably the most strange of the 3 types of smart pointers introduce in modern C++, in that they work with shared_ptr's. The idea is though, that the reference count is not increased, so that it is possible you can point to memory that has been freed. However, this can help solve some problems relating to simplifying ownership, breaking cycles, or 'more safely' being able to check for 'expired' memory.
►UA-cam Channel: / mikeshah
►Please like and subscribe to help the channel!
What I find them useful for is in STL containers. You can have a std::list< std::weak_ptr< some_type > >. If you're looping through that list, you can check if the reference object has been deleted via expired() and remove it from the list before doing anything with it.
Nice use case.
Thank you very much for your effort, Mike. You a one of the best teachers that I have ever met. Understanding lot of concepts used to be a disaster for me, but thanks to you I am able to easily comprehend everything.
P.S. I am surprised that such useful and easy-to-understand videos have so few likes and views. You definitely deserve way more!
Cheers, thank you for the kind words! The views will come in time 😜
HI Mike, Is there any exercise book on C++ with solved examples of smart pointers? Regards
This might be a question for Jason Turner (he has some great puzzler books with exercises) @cppweekly
I’m so glad that I found your channel
Cheers!
will not increase reference count and can be checked for dangling pointer, would a wild pointer do the same thing? I guess it has the advantage of not requiring delete, but is there other significant benefits?
The advantage is that the weak_ptr let's you know if the object was destroyed before attempting to access it. weak_ptr can also be scoped more easily as well.
I am not much familiar with some new c++ concepts
Hope this playlist helps! :) The 3 types of smart pointers included in the Standard Template Library are one of the more used features of modern C++ (C++11 and beyond)
So whenever we wan to check the validity of the weak ptr do we use the expired method ?
For single-threaded applications you could use .expired to check the state. Better to use .lock first to ensure it exists however if you have a multi threaded application.
Great video, thanks for sharing
Thank you for the kind words!
Love the session, one quick question, what is the code editor you are using?
Cheers! I'm using VIM :)
Great video. Thank you.
Cheers, thank you for the kind words!
What does it mean to be owning vs non-owning pointer? Does owning refer to where the raw pointer's lvalue exists?
Ownership the idea of understanding which object is responsible for allocation and deletion. Usually if that happens within an object, we'd say that object owns the pointer and should also free it (unless explicitly transferred with 'move'). Thinking about it in terms of lvalue is useful for lifetimes of objects and the lifetime of memory.
Merci beaucoup