HOME

TheInfoList



OR:

In the
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
programming language, static_cast is an operator 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 value ...
.


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 In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new classe ...
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 the ...
); * converting numeric data types such as enums to
ints Ints is a Latvian language, Latvian masculine given name. It is a short form of Indriķis, the Latvian form of Henry (given name), Henry, and may refer to: * Ints Cālītis (born 1931), Latvian politician and former political prisoner * Ints D ...
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 progra ...
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 typ ...
* reinterpret_cast *
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 valu ...


References

{{DEFAULTSORT:Static Cast C++ Type theory Articles with underscores in the title