dynamic_cast operator

   
dynamic_cast operator Polymorphic cast of class type objects

  postfix-expr  := dynamic_cast <  type-id  > (  expression  ) 

The dynamic_cast operator performs a polymorphic cast on a pointer or reference to an object of class type. If expression is a pointer or reference to a base class, type-id can be a pointer or reference to a derived class. If the dynamic type of the object is not that of type-id or a class derived from type-id , the cast fails. If expression is a pointer, failure returns a null pointer; if expression is a reference, failure throws std::bad_cast . Casts from a derived to an unambiguous, accessible base class always succeed.

If expression is a null pointer, the result is a null pointer. If type-id is a pointer to void , the cast succeeds and returns a pointer to the most-derived object that expression represents.

Example

 struct base {}; struct derived : base {}; base* b = new derived; derived* d =  dynamic_cast  <derived*>(b); 

See Also

const_cast , expression , reinterpret_cast , static_cast , type , Chapter 3, <typeinfo>

   


C++ in a Nutshell
C++ in a Nutshell
ISBN: 059600298X
EAN: 2147483647
Year: 2005
Pages: 270
Authors: Ray Lischner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net