Dynamic cast
Encyclopedia
In the C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 programming language, the dynamic_cast operator is a part of the run-time type information
Run-time type information
In programming, RTTI refers to a C++ system that makes information about an object's data type available at runtime. Run-time type information can apply to simple data types, such as integers and characters, or to generic objects...

 (RTTI) system that performs a typecast
Type conversion
In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations...

. Unlike an ordinary C-style
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 typecast, a type safety check is performed at runtime, and if the types are not compatible, an exception
Exception handling
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution....

 will be thrown (when dealing with references) or a null pointer will be returned (when dealing with pointers). In this regard, dynamic_cast behaves like a Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 typecast.

Example code

Suppose some function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 takes an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 of type A as its argument, and wishes to perform some additional operation if the object passed is an instance of B, a subclass of A. This can be accomplished using dynamic_cast as follows.

  1. include // For std::bad_cast
  2. include // For std::cerr, etc.


class A
{
public:
// Since RTTI is included in the virtual method table there should be at least one virtual function.
virtual void foo;

// other members...
};

class B : public A
{
public:
void methodSpecificToB;

// other members.
};

void my_function(A& my_a)
{
try
{
B& my_b = dynamic_cast(my_a);
my_b.methodSpecificToB;
}
catch (const std::bad_cast& e)
{
std::cerr << e.what << std::endl;
std::cerr << "This object is not of type B" << std::endl;
}
}


A similar version of my_function can be written with pointers instead of references
Reference (C++)
In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C...

:


void my_function(A* my_a)
{
B* my_b = dynamic_cast(my_a);

if (my_b != NULL)
my_b->methodSpecificToB;
else
std::cerr << "This object is not of type B" << std::endl;


}

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK