Type casting
C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion, known in C++ as type-casting. There exist two main syntaxes for generic type-casting:functional and c-like:
|
|
The functionality of these generic forms of type-casting is enough for most needs with fundamental data types. However, these operators can be applied indiscriminately on classes and pointers to classes, which can lead to code that -while being syntactically correct- can cause runtime errors. For example, the following code compiles without errors:
|
|
The program declares a pointer to
Addition
, but then it assigns to it a reference to an object of another unrelated type using explicit type-casting:
|
|
Unrestricted explicit type-casting allows to convert any pointer into any other pointer type, independently of the types they point to. The subsequent call to member
result
will produce either a run-time error or some other unexpected results.In order to control these types of conversions between classes, we have four specific casting operators:
dynamic_cast
,reinterpret_cast
, static_cast
and 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)
The traditional type-casting equivalents to these expressions would be:
(new_type) expression
new_type (expression)
but each one with its own special characteristics:
No comments:
Post a Comment