Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Please help me. Where to use <dynamic_cast> in c++ with example?

Please help me. Where to use <dynamic_cast> in c++ with example?

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shaktikanta
    wrote on last edited by
    #1

    behavior of with abase/derived scenario and how it is useful.thanks in advance.

    J S 2 Replies Last reply
    0
    • S shaktikanta

      behavior of with abase/derived scenario and how it is useful.thanks in advance.

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      here[^], here[^], maybe here[^] I'm sure there are examples all over the web.

      C S 2 Replies Last reply
      0
      • J jeron1

        here[^], here[^], maybe here[^] I'm sure there are examples all over the web.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        jeron1 wrote:

        I'm sure there are examples all over the web.

        I'm sure you are pretty right.

        Veni, vidi, vici.

        1 Reply Last reply
        0
        • S shaktikanta

          behavior of with abase/derived scenario and how it is useful.thanks in advance.

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          In general I would suggest you don't use it, having to tends to suggest you have some badly designed code. Prefer inheritance and virtual functions.

          Steve

          J 1 Reply Last reply
          0
          • S Stephen Hewitt

            In general I would suggest you don't use it, having to tends to suggest you have some badly designed code. Prefer inheritance and virtual functions.

            Steve

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            I concur with Stephen. Almost every example I've seen is contrived. There are a few exceptions, though with some of those better design would prevent having to use it.

            1 Reply Last reply
            0
            • J jeron1

              here[^], here[^], maybe here[^] I'm sure there are examples all over the web.

              S Offline
              S Offline
              shaktikanta
              wrote on last edited by
              #6

              dynamic_cast must be useful in parent child relation of classes. It helps to determine object type in a hierarchy. i.e - class Base{ }; class Derived1:public Base{ void der1Fun(){} }; class Derived2:public Base{ }; int main() { Base *bPtr = new derived1(); //now we want to call a derived1 function let der1Fun() Derived1* dPtr = 0; if( dPtr = dynamic_cast(dPtr) ){ dPtr->der1Fun() } else{ //Display the object is not of derived1 type } return 0; } I think this is why we need dynamic_cast.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups