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.
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!
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.
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.
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.
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)
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.
I’m so glad that I found your channel
Cheers!
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 😜
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!
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.
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.
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
Great video, thanks for sharing
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
👍!
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)