dynamic_cast uses?

This is a discussion on dynamic_cast uses? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am learning C++ and came across the topic dynamic_cast. I was able to understand what dynamic_cast means and ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    dynamic_cast uses?

    Hi,

    I am learning C++ and came across the topic dynamic_cast. I was able to understand what dynamic_cast means and syntax through sample examples in "C++ Complete Reference book".

    But I could not make out the uses of dynamic_cast. One of the things we can do is cast derived pointer to Base pointer. When will such a scenario occur, can anyone who knows please explain me the application of dynamic_cast .

    Thanks
    cjjoy

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,172
    OOP is really not my thing... but let me give it a try. Please correct me if I am wrong.

    dynamic_cast is like the regular "brackets cast" with a safety check. It won't allow you to cast when it doesn't make sense.

    For example, assuming Dog and Bird are subclasses of Animal, you will only be able to cast a Animal* to a Dog* if the pointer really points to a Dog.

    A specific usage scenario -
    Code:
    void move(Animal *a) { //assuming a is either a Dog or a Bird
         Dog *b = dynamic_cast<Dog *>(a);
         if (b) { //cast succeeded
              b->run();
         } else {
              Bird *c = dynamic_cast<Bird *>(a);
              c->fly();
         }
    }

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The general idea of OOP is that you treat all objects of derived classes like they were objects of the base class, and do everything through virtual functions.

    But sometimes, you will have a special case where you actually need to know if an object is something more specific than the base class. That's where you use dynamic_cast.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    147
    dynamic_cast is a little more than just casting from the base to a derived type, which is one of it's most common uses.

    static_cast is performed at compile time, and is otherwise unaware of the object's internal layout at runtime.

    dynamic_cast is performed at runtime using rtti information. Unlike static_cast, the return from a dynamic_cast can be null, if the cast is invalid. This means it can be used as a runtime check to see if a cast is valid. It can only be performed on objects with virtual functions (even if it's only a virtual destructor).

    Also of considerable importance is the use of casting in an object created from multiple inheritance. The pointer returned form a dynamic cast may not match, bit for bit, the pointer given. This is because dynamic_cast may have to adjust the value in order to match the 'position' of the type to which a pointer is cast.

    Consider an object C derived from both A and B. When the object is created, the pointer you see when observing the object as a type C will be, in colloquial terms, the 'entire' object. Assuming A is the 'first' parent, like:

    Code:
    class C : public A, public B
    {
    };
    Code:
    memory layout of C should be something like
    
    [A data]
    [B data]
    [C data]
    Then if you use dynamic_cast to cast a pointer of C to an A, the pointer value should be the same, because A is at the "base most" position in the object layout. However, if you cast C to a B, the pointer you get would be at the position of B within the layout, which will be a little farther down from the position of A (that is, in memory, the data of A should appear first, followed by the data of B). Think of it this way, if B is a class that can be instantiated by itself (new B for example), then, if the pointer you got by casting C to B didn't change, the contents of A would still be stored before the content of B, and the result would be invalid.

    This notion of pointer adjustment is a requirement in casting multiply derived objects, and any casting must perform a runtime adjustment. You'll see this happen "under the hood" if you track the "this" pointer inside member calls on an object of type C.

    If you're debugging and watch, "this" will have a value in member functions of class C which will be adjusted when you call member functions in class B - but it will happen without your awareness unless you look for it. Optimization can simplify this, so you'll need to make this observation in a debug build - otherwise the compiler might obviate the necessity through it's own trickery.


    Dynamic casts have a runtime penalty, so you should favor designs that don't use casting whenever possible.
    Last edited by JVene; 05-20-2009 at 06:54 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by JVene
    Then if you use dynamic_cast to cast a pointer of C to an A, the pointer value should be the same, because A is at the "base most" position in the object layout. However, if you cast C to a B, the pointer you get would be at the position of B within the layout, which will be a little farther down from the position of A (that is, in memory, the data of A should appear first, followed by the data of B).
    That said, non-POD class layout is implementation defined, so your mileage may vary, even though the point about pointer adjustment would nonetheless hold true.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    10
    Awesome....crystal clear.

    Thanks guys for all your explanations....

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21