ais promoted from
intwithout the need of any explicit operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow the conversions between numerical types (
int...), to or from
bool, and some pointer conversions.
intfrom some smaller integer type, or to
floatis known as promotion, and is guaranteed to produce the exact same value in the destination type. Other conversions between arithmetic types may not always be able to represent the same value exactly:
-1becomes the largest value representable by the type,
-2the second largest, ...).
falseequivalent to zero (for numeric types) and to null pointer (for pointer types);
trueis equivalent to all other values and is converted to the equivalent of
operatorkeyword followed by the destination type and an empty set of parentheses. Notice that the return type is the destination type and thus is not specified before the
B, but it could as well be called with an object of type
explicitcannot be called with the assignment-like syntax; In the above example,
barcould not have been constructed with:
explicit. This prevents implicit conversions in the same way as
explicit-specified constructors do for the destination type.
Addition, but then it assigns to it a reference to an object of another unrelated type using explicit type-casting:
resultwill produce either a run-time error or some other unexpected results.
const_cast. Their format is to follow the new type enclosed between angle-brackets (
<>) and immediately after, the expression to be converted between parentheses.
dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)
dynamic_castcan only be used with pointers and references to classes (or with
void*). Its purpose is to ensure that the result of the type conversion points to a valid complete object of the destination pointer type.
dynamic_castcan also downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members) if -and only if- the pointed object is a valid complete object of the target type. For example:
Null pointer on second type-cast.
Compatibility note: This type of |
pbb) to a pointer object of type
Derived*, but only the first one is successful. Notice their respective initializations:
pbaactually points to an object of type
pbbpoints to an object of type
Base. Therefore, when their respective type-casts are performed using
pbais pointing to a full object of class
pbbis pointing to an object of class
Base, which is an incomplete object of class
dynamic_castcannot cast a pointer because it is not a complete object of the required class -as in the second conversion in the previous example- it returns a null pointer to indicate the failure. If
dynamic_castis used to convert to a reference type and the conversion is not possible, an exception of type
bad_castis thrown instead.
dynamic_castcan also perform the other implicit casts allowed on pointers: casting null pointers between pointers types (even between unrelated classes), and casting any pointer of any type to a
static_castcan perform conversions between pointers to related classes, not only upcasts (from pointer-to-derived to pointer-to-base), but also downcasts (from pointer-to-base to pointer-to-derived). No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, it does not incur the overhead of the type-safety checks of
bwould point to an incomplete object of the class and could lead to runtime errors if dereferenced.
static_castis able to perform with pointers to classes not only the conversions allowed implicitly, but also their opposite conversions.
static_castis also able to perform all conversions allowed implicitly (not only those with pointers to classes), and is also able to perform the opposite of these. It can:
void*to any pointer type. In this case, it guarantees that if the
void*value was obtained by converting from that same pointer type, the resulting pointer value is the same.
static_castcan also perform the following:
enum classvalues into integers or floating-point values.
void, evaluating and discarding the value.
reinterpret_castconverts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.
intptr_t), is guaranteed to be able to be cast back to a valid pointer.
reinterpret_castbut not by
static_castare low-level operations based on reinterpreting the binary representations of the types, which on most cases results in code which is system-specific, and thus non-portable. For example:
bpoints to an object of a totally unrelated and likely incompatible class. Dereferencing
typeidallows to check the type of an expression:
type_infothat is defined in the standard header
<typeinfo>. A value returned by
typeidcan be compared with another value returned by
!=or can serve to obtain a null-terminated character sequence representing the data type or class name by using its
a and b are of different types: a is: int * b is: int
typeidis applied to classes,
typeiduses the RTTI to keep track of the type of dynamic objects. When
typeidis applied to an expression whose type is a polymorphic class, the result is the type of the most derived complete object:
a is: class Base * b is: class Base * *a is: class Base *b is: class Derived
type_infodepends on the specific implementation of your compiler and library. It is not necessarily a simple string with its typical type name, like in the compiler used to produce this output.
typeidconsiders for pointers is the pointer type itself (both
bare of type
class Base *). However, when
typeidis applied to objects (like
typeidyields their dynamic type (i.e. the type of their most derived complete object).
typeidevaluates is a pointer preceded by the dereference operator (
*), and this pointer has a null value,