In the
C++ programming language,
static_cast
is an
operator
Operator may refer to:
Mathematics
* A symbol indicating a mathematical operation
* Logical operator or logical connective in mathematical logic
* Operator (mathematics), mapping that acts on elements of a space to produce elements of another sp ...
that performs an explicit
type conversion
In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point val ...
.
Syntax
static_cast (object);
The ''type'' parameter must be a data type to which ''object'' can be converted via a known method, whether it be a builtin or a cast. The type can be a reference or an enumerator.
All types of conversions that are well-defined and allowed by the compiler are performed using
static_cast
.
The
static_cast<>
operator can be used for operations such as:
* converting a pointer of a
base class to a pointer of a
non-virtual derived class (
downcasting
In class-based programming, downcasting, or type refinement, is the act of casting a ''base'' or ''parent'' class reference, to a more restricted '' derived class'' reference. This is only allowable if the object is already an instance of the der ...
);
* converting numeric data types such as
enums to
ints or
floats.
Although
static_cast
conversions are checked at compile time to prevent obvious incompatibilities, no
run-time type checking is performed that would prevent a cast between incompatible data types, such as pointers. A
static_cast
from a pointer to a class
B
to a pointer to a derived class
D
is ill-formed if
B
is an inaccessible or ambiguous base of
D
. A
static_cast
from a pointer of a virtual base class (or a base class of a virtual base class) to a pointer of a derived class is ill-formed.
See also
*
dynamic cast
In computer programming, run-time type information or run-time type identification (RTTI) is a feature of some programming languages (such as C++, Object Pascal, and Ada) that exposes information about an object's data type at runtime. Run-time ...
*
reinterpret_cast
*
const_cast
References
{{DEFAULTSORT:Static Cast
C++
Type theory
Articles with underscores in the title