Dynamic cast
Encyclopedia
In the C++
programming language, the
(RTTI) system that performs a typecast
. Unlike an ordinary C-style
typecast, a type safety check is performed at runtime, and if the types are not compatible, an exception
will be thrown (when dealing with references) or a null pointer will be returned (when dealing with pointers). In this regard,
typecast.
takes an object
of type
A similar version of
:
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 informationRun-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 JavaJava (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 functionSubroutine
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.A similar version of
my_function
can be written with pointers instead of referencesReference (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...
: