Smart Pointers in C++ (Stop Using new?)

Sdílet
Vložit
  • čas přidán 28. 06. 2024
  • Start your software dev career - calcur.tech/dev-fundamentals Be notified of new C/C++ course: calcur.tech/c-cpp-newsletter
    Smart pointers can be a complicated subject, especially because there are multiple types! In this video we will talk about when you should use a smart pointer, and how each type works.
    💯 FREE Courses (100+ hours) - calcur.tech/all-in-ones
    Timestamps:
    00:00 - Intro
    00:45 - Old School Pointers
    02:00 - When do you Need Pointers
    03:53 - use New
    05:27 - Shared Pointers
    07:30 - Other Smart Pointers
    08:56 - Unique Pointer
    09:39 - Smart Pointer Functions
    11:45 - Weak Pointers
    13:17 - Dereferencing a Weak Pointer
    14:57 - Circular Reference
    15:46 - Review
    ⚛️ FREE React Course (20 hours) - calcur.tech/free-react-course
    ✅ Data Structures & Algorithms - calcur.tech/dsa-youtube
    ~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
    ✉️ Newsletter - calcur.tech/newsletter
    📸 Instagram - / calebcurry
    🐦 Twitter - / calebcurry
    🔗 LinkedIn - / calebcurry
    ▶️ Subscribe - calcur.tech/subscribe
    👨🏻‍🎓 Courses - www.codebreakthrough.com
    ~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
    ↪ My Amazon Store - www.amazon.com/shop/calebcurry
    🅿 Patreon - calcur.tech/patreon-calebcurry
    🅖 GitHub Sponsors - github.com/sponsors/CalebCurry
    Ⓟ Paypal - paypal.me/calcur
  • Věda a technologie

Komentáře • 32

  • @codebreakthrough
    @codebreakthrough  Před rokem +7

    I hope this was helpful! Here is the course link I mentioned in the video. New C/C++ course: calcur.tech/c-cpp-newsletter

    • @vinniciusrosa8284
      @vinniciusrosa8284 Před 5 měsíci

      I do not know yet how to pass unique_ptr as reference to a function.
      void whatever(...,std::unique_ptr &engine);
      - - -
      The call: whatever(engine).
      It should work, but do not compile.

  • @Nathan00at78Uuiu
    @Nathan00at78Uuiu Před rokem +4

    let's go. This is what we need. Been using your lessons for learning anything. So far been great. thanks.

  • @Sluggernaut
    @Sluggernaut Před 2 měsíci +2

    11:27 cracked up so hard. I do this all of the time when making videos.
    Edit: wth? How did that even compile at 13:12. You have to declare the data type when making a shared pointer. Weird.

  • @aminramazanifar9743
    @aminramazanifar9743 Před rokem +1

    As great as always. Cannot wait to see the course.
    Thanks!

  • @andreasguettinger6706

    As somebody who learned and uses mainly C++98, I'm glad to see videos about these newer C++ concepts I often forget about. 👍

  • @billbez7465
    @billbez7465 Před 5 měsíci

    Very well done. There were other videos on smart pointers that left me a bit confused. Yours is clear & understandable. Thank you

  • @MichaelCohen-sb7tw
    @MichaelCohen-sb7tw Před 3 měsíci

    Thanks Caleb. Very clear examples and explanations.

  • @xbiohazardx7312
    @xbiohazardx7312 Před rokem

    This video is very helpful, it's make me figured it out in one video huge thanks!

  • @ArtVandelayInc
    @ArtVandelayInc Před 9 měsíci

    Great tutorial! Thank you

  • @alirezaarttam3344
    @alirezaarttam3344 Před rokem

    Awsome. Thanks!

  • @suryadon947
    @suryadon947 Před 5 měsíci

    Best video for smart pointers
    Great bro🎉

  • @dwivedys
    @dwivedys Před měsícem

    Hey good presentation Caleb!

  • @nokia_n-gage
    @nokia_n-gage Před 8 měsíci

    Thanks!

  • @fpldaily5172
    @fpldaily5172 Před rokem

    hey bro. Please do a java springboot video or course?

  • @furkankaraslan9040
    @furkankaraslan9040 Před rokem +1

    so unique allows one owner
    shared allows unlimited owner
    and weak one makes 0 owner at total and we need least one owner to keep scoped value alive, did i understand correctly? if i am not someone correct me please.

  • @user-ln2uo5vi8e
    @user-ln2uo5vi8e Před 10 měsíci

    This is one of the best coding tutorial videos I've watched. Clear, concise and complete. Thank you so much for your great work!

  • @midewestmond9442
    @midewestmond9442 Před 9 měsíci

    Why you using camel case btw and more can you make a video how to setup an editor for c++ development

  • @abhishekkhanchi9105
    @abhishekkhanchi9105 Před rokem +1

    Hi Caleb
    i ran shared pointer example.its giving error .
    error:smart_p3.cpp:6:34: error: conversion from ‘’ to non-scalar type ‘std::shared_ptr’ requested
    6 | std::shared_ptr a= std::make_shared;
    during compilation . which c++ compiler ur using..
    I tried this on g++: gcc version 9.4.0
    Please guide
    Thanks
    Abhishek

    • @codebreakthrough
      @codebreakthrough  Před rokem +3

      You have it should be (5)
      It looks like it thinks you are trying to use a function for the type because you put the () inside the

    • @abhishekkhanchi9105
      @abhishekkhanchi9105 Před rokem

      thanks alot caleb(@@codebreakthrough ) it work for me..

  • @user-hh4du9ry9g
    @user-hh4du9ry9g Před 4 měsíci

    cystall clear!

  • @CCCole1
    @CCCole1 Před rokem

    P.S. Love the content

  • @Antagon666
    @Antagon666 Před 10 měsíci +1

    The level of abstraction is on a really akward level.
    On one hand yeey nice, you don't have to delete pointers manually.
    On the other hand, if you need to do anything more advanced like cloning, deep and or shallow copy and storing in containers, that requires even more work, knowledge and frustration than managing raw memory directly.
    I find using smart pointers in very simple situations, where it's not necessary at all and in complicated situations I use raw pointers, just because I hate thinking about the obscure behavior of smart pointers.

    • @snesmocha
      @snesmocha Před 8 měsíci

      Isn’t that what smart pointers?

    • @KaiDevvy
      @KaiDevvy Před 6 měsíci

      Smart pointers are a fairly thin wrapper in terms of added complexity. It's not a huge difference in understanding to use them as opposed to raw pointers, and the benefits of clearly defined ownership and automatic deallocation is monumental for the little abstraction.
      To me though, something that bothers me is just how verbose it is. Going from * to std::shared_ptr is a pain in the ass. Especially when you're making collections of smart pointers or the like

  • @atrizzstinks
    @atrizzstinks Před 5 měsíci

    with automatic memory safety comes overhead

  • @sent4dc
    @sent4dc Před rokem

    4:39 WRONG! 2 memory leaks.

  • @hawks3109
    @hawks3109 Před 9 měsíci

    Don't use auto in production code, the rest is good, but auto can make bad assumptions when you start getting into nested data types and other techniques that come up in production code. auto is dangerous and could lead to bad assumptions by the compiler.

  • @zohnannor
    @zohnannor Před rokem +4

    bro thinks hes mcoding💀💀