dynamic_cast In C++

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • JOIN ME
    -----
    CZcams 🎬 / @cppnuts
    Patreon 🚀 / cppnuts
    COMPLETE PLAYLIST
    ------------
    C++ Tutorial For Beginners: • Introduction To C++
    STL (Standard Template Library): • STL In C++
    ThreadIng In C++: • Multithreading In C++
    Data Structures: • Data Structure
    Algorithms: • Binary Search
    Design Patterns: • Factory Design Pattern...
    Smart Pointers: • Smart Pointer In C++
    C++14: • Digit Separator In C++
    C++17: • std string_view in C++...
    C++ All Type Casts: • static_cast In C++
    INTERVIEW PLAYLIST
    ------------
    C++ Interview Q&A: • Structural Padding & P...
    C++ Interview Q&A For Experienced: • How delete[] Knows How...
    Linked List Interview Questions: • Find Kth Node From Bac...
    BST Interview Questions: • Search Element In Bina...
    Array Interview Questions: • Reverse An Array
    String Interview Questions: • Check String Is Palind...
    Bit Manipulation Questions: • Find Set Bit In Intege...
    Binary Tree Interview Question: • Invert Binary Tree
    Sorting Algorithms: • Bubble Sort
    C++ MCQ: • Video
    C MCQ: • What printf returns af...
    C Interview Questions: • Designated Initializat...
    QUICK SHORT VIDEOS
    -------------
    C++ Short : • C++ Short Videos
    C Short : • Shorts C Programming MCQ
    Hey guys we will discuss dynamic_cast in this video and these are the important points i have noted for you guys about the dynamic_cast:
    0. dynamic_cast is used at run time to find out correct down-cast.
    1: Need at least one virtual function in base class.
    2: If the cast is successful, dynamic_cast returns a value of type new_type.
    3: If the cast fails and new_type is a pointer type, it returns a null pointer of that type.
    4: If the cast fails and new_type is a reference type, it throws an exception that matches a handler of type std::bad_cast.
    BOTTOM LINE:
    1. work only on polymorphic base class (at least one virtual function in base class) because it uses this information to decide about wrong down-cast.
    2. it is used for up-cast (D to B) and down-cast (B to D), but it is mainly used for correct downcast.
    3. using this cast has run time overhead, because it checks object types at run time using RTTI (Run Time Type Information).
    4. if we are sure that we will never cast to wrong object then we should always avoid this cast and use static_cast.
    #dynamic_cast #cpp #programming #interviewquestions #tutorial #computerscience #softwareengineering

Komentáře • 57

  • @CppNuts
    @CppNuts  Před 5 lety +6

    Hi everyone, Don't forget to hit LIKE and SUBSCRIBE button for more videos like this!!
    And this will help me a-lot.

  • @stanlythomas7140
    @stanlythomas7140 Před 3 lety +1

    What is the difference between the following ?
    Base* b = new Derived()
    and
    Derived d1;
    Base* b = static_cast(&d1);

    • @CppNuts
      @CppNuts  Před 3 lety

      You can't delete later.

  • @Byynx
    @Byynx Před 11 měsíci +1

    With Microsoft VS compiler when down-casting it puts a squiggle bellow the cast warning us. No need to check wit extra code i think.

  • @jamesdvc
    @jamesdvc Před 4 lety

    this video gave me the best explanation i ever found. the examples and details covered are so insightful! much appreciate your work and please keep it up. Thank you!

  • @sahasrakasukurthy1364
    @sahasrakasukurthy1364 Před 2 lety

    Now I am clear about dynamic cast. Thank you so much Rupesh.

  • @andreiasimov33
    @andreiasimov33 Před 2 lety +1

    Simply marvelous, thanks a lot for the presentation and clear explanation.
    You are the best!!

  • @mikusodaclub
    @mikusodaclub Před 4 lety +1

    You sir are a good teacher, thank you! have a blessed day !

  • @karthickraj6580
    @karthickraj6580 Před 4 lety +1

    Precise explanation sir, thank you. BTW what is the editor you are using sir?

    • @CppNuts
      @CppNuts  Před 4 lety +1

      Sublime Test Editor.

  • @SimplySpiceIt
    @SimplySpiceIt Před 6 lety +6

    In the base, you need a virtual destructor as well.

    • @CppNuts
      @CppNuts  Před 6 lety

      Yes!!

    • @treyquattro
      @treyquattro Před 5 lety +4

      Not exactly. As per Stroustrup (17.2.5, 4th Ed) you *should* have a virtual destructor if your hierarchy uses virtual functions. However, obviously from the examples in the video you don't *need* a virtual destructor.

  • @GauravGupta-zw7hz
    @GauravGupta-zw7hz Před 3 lety

    Sir, You are great. Best source for CPP videos :)

  • @aristotales4906
    @aristotales4906 Před 6 lety +3

    it helped me, thanks.

  • @01MeuCanal
    @01MeuCanal Před 4 lety +1

    Very good tutorial. Thank you.

  • @TarunKumar-hi5xk
    @TarunKumar-hi5xk Před 5 lety +1

    Your videos are great. Thanks. Can you also keep a blog of the content you present in your videos? I did not find one. It's easier to make notes that way.

    • @CppNuts
      @CppNuts  Před 5 lety +1

      Tarun, it takes so much effort to write (i tried once) and i don't have much time. Thanks for suggestion man!!

  • @joshposh7685
    @joshposh7685 Před 4 lety +1

    Why couldn't you cast the base class pointer bp to the Derived2 class at 4:57?
    My understanding is that your first dynamic cast is performed from Derived1 object d1 to make it type Base, made the base pointer bp to point to Base d1, and since d1 was a derived1 object, the second dynamic cast only works for Derived1. Is any of this wrong?

    • @CppNuts
      @CppNuts  Před 4 lety

      The simplest way to tell this is, when we have relationship like
      class Derived1: Base
      class Derived2: Base
      then some time we can type cast Derived1 to Derived2 or vise versa with the help of Base pointer or reference, if we will not use dynamic_cast.
      If you will watch this video again and till last you may get it.

  • @utkalikasamal5067
    @utkalikasamal5067 Před 5 lety +1

    This is a great video .Thanks

    • @CppNuts
      @CppNuts  Před 5 lety

      Thanks for the comment dude!!

  • @ashj1165
    @ashj1165 Před 5 lety +2

    short and up to the point... :)

  • @thewanderingrooh
    @thewanderingrooh Před 6 lety +2

    Nice Explanation..

  • @sivaramakrishnachitithoti839

    excellent ...! I got it..

  • @elyxthelynx
    @elyxthelynx Před 5 lety +1

    Very good video!

  • @Hack_Neuron_To_DSA
    @Hack_Neuron_To_DSA Před 4 lety

    Want to tell you there may be some technical issue with playback. When am trying to skip for or back then it just waiting not playing.
    If some more people is facing that then raise issue with technical team

  • @theradhikagupta
    @theradhikagupta Před 5 lety

    Sir why can't we perform static and dynamic cast when the base class is inherited privately??please explain what happened internally.

  • @subbumotepalli
    @subbumotepalli Před 5 lety

    Plz provide virtual pointer & virtual tables concept against virtual functions.

  • @ayeshafatima3012
    @ayeshafatima3012 Před 3 lety

    If we have only one derived class than there is use of down cast or not plz reply???

  • @sudheerkumar4036
    @sudheerkumar4036 Před 5 lety +1

    Hi, other than 6 design patterns could you please keep more patterns in c++

  • @RealRobotZer0
    @RealRobotZer0 Před 5 lety +1

    what is the ide compiler program you are using?

    • @CppNuts
      @CppNuts  Před 5 lety

      It is sublime text editor 3. Its just text editor, you have to provide compiler yo it. Google it u will get.

  • @messiisthebest
    @messiisthebest Před 3 lety

    please use another editor it is hard to read on that theme or download new theme

  • @prishaphotography9063
    @prishaphotography9063 Před 6 lety +2

    Super boss

  • @Sgoose105
    @Sgoose105 Před 5 lety +1

    Why are there so many ads in a 10 min video

    • @CppNuts
      @CppNuts  Před 5 lety

      Sorry for the inconvenience, It depends on google, how much ads it actually want to consider, from given position by me.

    • @Hack_Neuron_To_DSA
      @Hack_Neuron_To_DSA Před 4 lety

      Thats shows how great his content is. And how much they believe these gonna benefit their cost of advertisement

  • @prasadreddy443
    @prasadreddy443 Před 5 lety +1

    super sir,nice efforts keep it up

  • @stephenj.wiener9961
    @stephenj.wiener9961 Před 2 lety

    Do you have a link to where you post the code in the video?

  • @jensindalmout4866
    @jensindalmout4866 Před 2 lety

    Dude. Can’t understand you. Have some else talk or something.