- Dynamic cast
In the
C++ programming language, thedynamic_cast
operator is a part of therun-time type information (RTTI) system that performs a typecast. However, unlike an ordinary C-style typecast, a type safety check is incurred atruntime , and it will either throw an exception (when dealing with references) or return anull pointer (when dealing with pointers) if the types are not compatible. Thus,dynamic_cast
behaves more like a typecast in a programming language such as Java, rather than the C-style casting which performs no runtime check.Example code
Suppose some function takes an object of type
A
as its argument, and wishes to perform some additional operation if the object passed is actually an instance ofB
, a subclass ofA
. This can be accomplished usingdynamic_cast
as follows.A similar version of
my_function
can be written with pointers instead of references:External links
* [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc05keyword_dynamic_cast.htm
dynamic_cast
operator at IBM Mac OS X Compilers]
* [http://msdn2.microsoft.com/en-us/library/cby9kycs.aspxdynamic_cast
operator at MSDN]
Wikimedia Foundation. 2010.