Classes part 9 - Resource Acquisition is Initialization (RAII) Ep. 45

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024

ความคิดเห็น • 39

  • @bluehornet6752
    @bluehornet6752 หลายเดือนก่อน +1

    Nice video, I especially like the reminder that the destructor is guaranteed to be called if an exception is thrown But I do have a question for you...
    Why are you returning an 8-byte integer reference from your operator[] overload(s), when you can simply return the (probably) 4-byte integer? Basically, I guess I'm wondering what the benefit is to returning reference to a primitive type in this case? Are you planning to allow method chaining or something? What would be the benefit(s) of doing it the way you did?

    • @MikeShah
      @MikeShah  หลายเดือนก่อน +1

      In this particular instance you're right returning the value (4-byte integer) would be more optimal. For the strategy shown, returning the reference allows you to do: collection[idx] = some_value; // i.e. You can do the assignment by returning the reference.

  • @Sergei_developer
    @Sergei_developer 2 หลายเดือนก่อน +1

    Great lesson as always. I'm just curious if there are such excellent resources for mastering the Assembly language. Thank you, sir for this amazing content and your precious time. Indeed, you deserve tremendous respect and many more views.

    • @MikeShah
      @MikeShah  2 หลายเดือนก่อน +1

      Cheers! There should be many great assembly lessons on TH-cam -- at some point I'll update my own (th-cam.com/play/PLvv0ScY6vfd9BSBznpARlKGziF1xrlU54.html) -- I tend to look at university courses, or otherwise compiler explorer to check assembly otherwise :)

  • @bernhardkneer5393
    @bernhardkneer5393 ปีที่แล้ว +1

    Hi Mike, again a great video!! This new C++ makes it more and more hard to understand what is going on in the background. e.g. from 6:00 onwards with the overloading of the [ ] operator... how the hell does this statement myCollection[0] = 7; change the private data value? Is it because the [ ] operator returns a reverence to the memory of this index and then writing to it works? Is this some "dirty" implementation or is this normal in modern C++? ;-)

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      Public interface can change the private underlying data -- the goal is to not change any private variables directly, but rather do it through the operator[]. This is true in programming languages. The reason for having both versions, is that one is for read access (i.e. const), and one is for writing a new value.

  • @TheRawi
    @TheRawi 3 หลายเดือนก่อน +1

    Thank you man. You are very good at this 👍

    • @MikeShah
      @MikeShah  3 หลายเดือนก่อน

      Cheers!

  • @_w62_
    @_w62_ ปีที่แล้ว +1

    Thanks for another great video. Could you recommend any C++ based graphic libraries that make uses of RAII so that I can have a feel of actual use case of it?

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Cheers! Try SFML

  • @adityaanand4749
    @adityaanand4749 ปีที่แล้ว +1

    Good job Mike. I highly appreciate your crisp and point to point explanation. Keep it up 👍

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Thank you for the kind words!

  • @thestarinthesky_
    @thestarinthesky_ ปีที่แล้ว +1

    Great video! You're so awesome! references you're using and introducing in your channel for C++ says a lot how knowledgeable you're. Thanks for sharing with us.

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      Cheers! Thank you as always for the kind words!

  • @dibyojyotibhattacherjee897
    @dibyojyotibhattacherjee897 2 ปีที่แล้ว +2

    Hey Mike,will there be any cmake series?

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +3

      I have on my TODO list a series of ideas for a build systems series with 'shell scripts' 'make' 'cmake', and some other tools.

    • @dibyojyotibhattacherjee897
      @dibyojyotibhattacherjee897 2 ปีที่แล้ว

      @@MikeShah Thanks a lot for the heads up!

    • @Southpaw101
      @Southpaw101 2 ปีที่แล้ว +1

      @@MikeShah This will be really useful Mike. I have been waiting on something with cmake and Vcpkg. Would really appreciate it and Thanks again for all these classes.

  • @muhammedhasankayapnar9495
    @muhammedhasankayapnar9495 ปีที่แล้ว +1

    Hi Mike, thank you for this series. I learnt a lot as a person who just has python programming background. For large scale data processing, is there any chance also to introduce us CUDA programming as well with C++ ?

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Cheers -- thank you for the kind words! I'll consider something like this in the future. I tend to lean towards OpenCL for compute as it is cross platform (though I've used both)

  • @thestarinthesky_
    @thestarinthesky_ ปีที่แล้ว

    Hi Mike. Thanks for this great tutorial. By any chance do you think this tutorial can help me to nail a c++ interview questions? or do I need to know more?

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      Cheers! I hope these videos certainly do help! Going through the series (including some of the 'interview portions') should help! As always, make sure to practice, and try building a few simple projects :)

  • @windowsbuilderthegreat3121
    @windowsbuilderthegreat3121 2 ปีที่แล้ว +1

    Are exceptions in C++ expensive like in C#

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +2

      Yes, relative to regular executing code. This blog has some performance numbers to show the relative scale (10x slower for code, or 100x slower for optimized code). pspdfkit.com/blog/2020/performance-overhead-of-exceptions-in-cpp/ I have not reproduced the experiment to confirm the numbers. For high performance systems (games, low latency trading, etc.) consider carefully if exceptions are the right tool for handling errors at run-time.

  • @Lopito_PP
    @Lopito_PP 2 ปีที่แล้ว

    Noob question, why is Collection[0] = 7 a memory leak? Is it because we are not doing the delete Collection in the end?

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +2

      I'm assuming you mean around the 3 minute mark? Because we never deallocate 'Collection' which is part of the 10 integers allocated on the heap.

  • @odaialkhodary7872
    @odaialkhodary7872 2 ปีที่แล้ว

    are you using mac with m1 chip? how can i detect memory leakage on mac with m1 chip ? i can't download valgrin.

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      I do have a Mac M1, and the tool to use is called 'leaks'. Look out for a video later this week on how to use leaks!

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      url will be: th-cam.com/video/bhhDRm926qA/w-d-xo.html (will be release by May 31, 2022)

  • @dhanushs1802
    @dhanushs1802 2 ปีที่แล้ว

    Wonderfully explained as always. Thank You.

  • @amitmulay729
    @amitmulay729 2 ปีที่แล้ว +2

    Thank you, I was waiting for this one. :)

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      Hehe you're most welcome Amit!

  • @dwolrdcojp
    @dwolrdcojp 2 ปีที่แล้ว +1

    Thanks Mike!

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      You are most welcome! 😁

  • @rinket7779
    @rinket7779 ปีที่แล้ว +1

    Bro I like your vids but this video is extremely misleading. You spend a large portion saying that destructors are still called if an exception is thrown in the constructor. This is just not true. Try it.
    The example you then show to illustrate your point catches the exception in the constructor before it has a chance to escape. This is of course the case, as no exception then escapes the constructor at all. So from the pov of the cstor no exception occurred at all. Making the entire point a bit silly 😅
    Other than that, good set of vids though!

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Cheers, can you give a timestamp?

    • @rinket7779
      @rinket7779 ปีที่แล้ว

      ​@@MikeShaharound 10:25

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      @@rinket7779 Ah yes, fair point -- yeah if the object has not yet been constructed, we will not call the destructor from the constructor in the case of an exception. Agreed showing the try/catch/finally within the constructor does not demonstrate that behavior! Perhaps a deep dive video into exceptions in the future I will amend this :)