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!

КОМЕНТАРІ • 23

  • @jaykemper
    @jaykemper Рік тому +6

    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.

  • @horsetv4532
    @horsetv4532 2 роки тому +2

    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!

    • @MikeShah
      @MikeShah  2 роки тому

      Cheers, thank you for the kind words! The views will come in time 😜

  • @syedimadhaqqi4340
    @syedimadhaqqi4340 5 місяців тому +1

    HI Mike, Is there any exercise book on C++ with solved examples of smart pointers? Regards

    • @MikeShah
      @MikeShah  5 місяців тому

      This might be a question for Jason Turner (he has some great puzzler books with exercises) @cppweekly

  • @andrew121410
    @andrew121410 Рік тому +3

    I’m so glad that I found your channel

  • @dizhang571
    @dizhang571 Рік тому +1

    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?

    • @MikeShah
      @MikeShah  Рік тому

      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.

  • @binhnguyenthanh5084
    @binhnguyenthanh5084 2 роки тому +1

    I am not much familiar with some new c++ concepts

    • @MikeShah
      @MikeShah  2 роки тому +2

      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)

  • @mav474
    @mav474 Рік тому +1

    So whenever we wan to check the validity of the weak ptr do we use the expired method ?

    • @MikeShah
      @MikeShah  Рік тому +1

      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.

  • @miketag449
    @miketag449 2 роки тому +2

    Great video, thanks for sharing

    • @MikeShah
      @MikeShah  2 роки тому

      Thank you for the kind words!

  • @ddebasis
    @ddebasis Рік тому +1

    Love the session, one quick question, what is the code editor you are using?

    • @MikeShah
      @MikeShah  Рік тому

      Cheers! I'm using VIM :)

  • @dhanushs1802
    @dhanushs1802 2 роки тому +2

    Great video. Thank you.

    • @MikeShah
      @MikeShah  2 роки тому

      Cheers, thank you for the kind words!

  • @damondouglas
    @damondouglas Рік тому

    What does it mean to be owning vs non-owning pointer? Does owning refer to where the raw pointer's lvalue exists?

    • @MikeShah
      @MikeShah  Рік тому +1

      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.

  • @joebosah2727
    @joebosah2727 Рік тому

    Merci beaucoup