Find the answer to your Linux question:
Results 1 to 3 of 3
In what situations we have to use RTTI dynamic_cast mechanism. What is the difference between dynamic_cast and static_cast First I have searched in net. But I didn't the right one. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    21

    dynamic_cast in RTTI C++


    In what situations we have to use RTTI dynamic_cast mechanism.
    What is the difference between dynamic_cast and static_cast

    First I have searched in net. But I didn't the right one. So, I am posting here.

    Thanks in Adavance,
    MAdhu

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,882
    dynamic_cast checks the known type information for the instance pointed at - if the instance could be of the type requested, a pointer to that type is returned. Otherwise it returns NULL. You'd usually use dynamic_cast to change a pointer to a class instance to another class in the instance's class heirarchy.

    static_cast just changes the type without making checks to see if anything is valid.

    If this is a homework question, then please disregard my answer and go ask your professor
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,558
    Of course, you can also do dynamic_cast<Type&> in which case you must be prepard to catch( bad_cast ). Sometimes books are better than the net for such stuff. Check out Bjarne Stroustrup's "C++ Programming Language". It will set you back about $65-$70 USD on Amazon.com.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •