![]() |
![]() |
|
![]() |
![]() |
Encyclopedia :
T :
TY :
TYP :
Type conversion |
|
|
Type conversion
General In computer science, type conversion or typecasting refers to changing an entity of one datatype into another. There are several types conversion. checked - unchecked - bit pattern - Each programming language has its own rules on how types can be converted. In general, both objects and fundamental data types can be converted. in Ada Ada supports all three convertion techniques and there is a full article on how to us them in . It might be worth reading even if you plan to use another programming language. in C/C++A cast, or explicit type conversion, is special programming instuction which specifies what data type to treat a variable as (or an intermediate calculation result) in a given expression. Casting will ignore "extra" information (but never adds information to the type being casted). The C/C++ cast is either "unchecked" or "bit pattern". As an example with fundamental data types, a fixed-point float could be cast as an integer, where the data beyond the decimal (or binary) point is ignored. Alternatively, an integer could be cast as a float if, for example, a function call required a floating point type (but, as noted, no information is really added - 1 would become 1.0000000). Object casting works in a similar way. A subclass can be cast as a parent type, where the "extra" information that makes it a subclass is ignored, and only the parts inherited from the parent are treated. For example, a triangle class derived from a shape class could be cast as a shape. Two common casting styles There are two common casting styles, each outlined below. C-style castingThis style of casting is used in C and Java. It follows the form: (type)expression C++-style castingSeveral cast syntaxes are used in C++ (although C-style casting is supported as well). The function-call style follows the form:type(expression) This style of casting was adopted to force clarity when using casting. For example, the result of, and intention of, the C style cast (type)firstVariable + secondVariable may not be clear, while the same cast using C++-style casting allows more clarity: type(firstVariable + secondVariable) or type(firstVariable) + secondVariable Later in the evolution of C++, the following more explicit casts were added to the language to clarify the programmer's intent even further: static_cast double myDouble = 3.0;
int myInt = static_cast YourClass * pYour = GimmeAnObject();
void * pv = pYour; // no cast needed.
MyClass * p = static_cast A dynamic cast is safer than a static cast in this scenario: it is compiled by the compiler into a call to the C++ runtime library where a check is made to ensure legal casts. This is analogous to the casts in Java. YourClass * pYour = GimmeAnObject();
void * pv = pYour; // no cast needed.
MyClass * p = dynamic_cast A const cast casts away the constness of an object, returning a non-const reference to the same object. This allows modifications to objects that normally would be treated read-only by the compiler: const MyClass * cantTouchThis = CreateConstObject();
cantTouchThis->constant_value = 41; // compile-time error.
const_cast MyClass * pclass = reinterpret_cast
|
|
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License. |
|
| © 2008 Chamas Enterprises Inc. |