C++ Casting - Part 3 - static_cast and dynamic_cast (mega lesson) | Modern Cpp Series Ep. 94

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.ย. 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 talk about the four keywords that were added to C++ 11, focusing primarily on static and dynamic cast. These named casts are generally more safe to use, easier to grep for, and can help you cast your data more safely when you absolutely must cast your data. I'll walk through several examples with a class hierarchy, and hopefully convince you that the named casts better show your intent of the code, and are much better than the C-style cast (which essentially is trying all of these named casts).
    ►TH-cam Channel: / mikeshah
    ►Please like and subscribe to help the channel!

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

  • @mba2ceo
    @mba2ceo 10 หลายเดือนก่อน +7

    GOD he is such a GREAT teacher - GOD DAMN ALL MY PROFESSORS !!!

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

      Thank you for the kind words 🙏

  • @joebosah2727
    @joebosah2727 19 วันที่ผ่านมา +1

    I love C++

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 6 หลายเดือนก่อน +2

    great explanation thank you so much
    please continue , we need more in depth tutorials

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

      Cheers -- the series continues (almost 200 videos as of today! :) )

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

    Before I watched this video, I was acting like the C-Style cast does: Keep trying stuff until something works. "oops, this compiler/program didn't like this one, lets try the next!". Another great video!
    Would also love if you mentioned std::static_pointer_cast and dynamic_pointer_cast with smart pointers in a subsequent video.

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

      Cheers, thanks Tom! Will add those topics to the future list!

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

    I did not know that C-style casts, and the other types of casts even existed.😅
    Thanks Mike!

  • @Gaspardkirira
    @Gaspardkirira 8 วันที่ผ่านมา +1

    I would like to be part of your online team once I have finished all the playlists and design patterns, as well as OpenGL

    • @MikeShah
      @MikeShah  8 วันที่ผ่านมา

      One of the best ways to get involved is to join the community forum at courses.mshah.io and ask/answer questions :)

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

    thank you

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

      Cheers!

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

    hallo Mike, we love to see the two other cast.
    Regards

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

      Noted! 😀

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

      Yes please!

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

    Great Explanation!

  • @kenk4552
    @kenk4552 2 หลายเดือนก่อน

    Hi Mike, why do you recommend using dynamic_cast to handle pointers and references at 30:39. It seems like static_cast works with pointers and references too, at around 20:00.

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

      I'll need to think about this a bit more, but here's a guideline:
      - prefer static_cast for performance, and use for numeric types (float, int, etc.).
      - prefer dynamic_cast if you want to handle an error if the cast does not work. Cannot use dynamic_cast on numeric types (e.g. int to float). It seems if you had a void* that a dynamic_cast is something you would want to use to check the type's validiity, whereas reinterpret_cast or static_cast could fail.
      Some other notes:
      - dynamic_cast generally considered 'slow' but safer. There's ways to get around the slowness, perhaps you could create a 'template' and have an assert or something to check if it's legal to do.
      From: learn.microsoft.com/en-us/cpp/cpp/static-cast-operator?view=msvc-170
      "In general you use static_cast when you want to convert numeric data types such as enums to ints or ints to floats, and you are certain of the data types involved in the conversion. static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does. A dynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if nothing were wrong; this can be dangerous. Although dynamic_cast conversions are safer, dynamic_cast only works on pointers or references, and the run-time type check is an overhead. For more information, see dynamic_cast Operator."

  • @peerajak
    @peerajak 7 หลายเดือนก่อน

    Thank you

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

      Cheers!