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 ...
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 valu ...
.
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 reference of a base class to one of its derived classes.
In many programming languages, it is possible to check through type introspection to determine whether t ...
);
* 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
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer program ...
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 t ...
*
reinterpret_cast
In computer science, a type punning is any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal ...
*
const_cast
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 v ...
References
{{DEFAULTSORT:Static Cast
C++
Type theory
Articles with underscores in the title