GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 140 of 203    Print  
What is the diffrence between static_cast and dynamic_cast in RTTI
dynamic_cast checks for datatype at runtime but static_cast checks at compilation. Is this the only diffrence b/w them or is there anything else? at the time of programming which shud we prefer to call.


  
Total Answers and Comments: 3 Last Update: February 05, 2008     Asked by: vikaspachauri 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Sushant Kadadi
 
static_cast is used for all conversions that are well defined.
Like float to int. double to int etc.

dynamic_cast is used for type-safe downcasting. For this one must be working with a truly polymorphic hierarchy.(i.e with virtual functions).
downcasting is a method to convert the base class pointer to a derived class pointer.
incorrect downcast will produce a zero result.

Above answer was rated as good by the following members:
es1968
January 10, 2008 06:53:40   #1  
wanttochange Member Since: January 2008   Contribution: 9    

RE: What is the diffrence between static_cast and dynamic_cast in RTTI
static_cast
1)can do explicit conversion
2)can do implicit conversion ....to avoid warnings
3)can do unsafe down casting
4)can convert a integer in to some enum variable

.......it can't use run time information

2)dynamic_cast
only use to do typesafe downcasting
suppoce we have pointer of parent class that have the address of some child class
& we want to access the derive class exclusive member ..then we need to downcast it
if that pointer contain the address of child then it will downcast the pointer
otherwise it will retun NULL & if instead a pointer we have reference then it will through bad_typeid exception

 
Is this answer useful? Yes | No
January 23, 2008 13:09:40   #2  
ranjini_r Member Since: October 2006   Contribution: 1    

RE: What is the diffrence between static_cast and dynamic_cast in RTTI
static_casts can be used to convert parent class to child class and vice - versa but the cast is an error for "unrelated classes".static casts can also be used to convert integral types to enum and vice-versa(enum can be converted to integral type cast without a type cast though).

the expression pChild dynamic_cast<Child*>(pParent);
converts pointer pParent to type Child only if the pointed to object(i.e *pParent) is of
type Child or directly/indirectly derived from Child.The expression evaluates to null otherwise.Incase of references dynamic_cast throws bad_cast exception defined in typeinfo header file. Incase you want to call an uninherited method of the derived class during runtime dynamic_cast is better as it will return null pointer if you cant safely assign the address of object to the pointer of Type Child.If you use
pChild static_cast<Child*>(pParent); calling an uninherited method would be unsafe.I've never tried this though...

 
Is this answer useful? Yes | No
February 05, 2008 10:08:15   #3  
Sushant Kadadi Member Since: February 2008   Contribution: 2    

RE: What is the diffrence between static_cast and dynamic_cast in RTTI
static_cast is used for all conversions that are well defined.
Like float to int. double to int etc.

dynamic_cast is used for type-safe downcasting. For this one must be working with a truly polymorphic hierarchy.(i.e with virtual functions).
downcasting is a method to convert the base class pointer to a derived class pointer.
incorrect downcast will produce a zero result.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape