Type conversion

This is an old revision of this page, as edited by SwiftBot (talk | contribs) at 01:57, 13 January 2010 (Robot: Automated text replacement (-{{Wikibookspar|Transwiki| +{{Wikibooks|Transwiki:)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer science, type conversion or typecasting refers to changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies. For instance, values from a more limited set, such as integers, can be stored in a more compact format and later converted to a different format enabling operations not previously possible, such as division with several decimal places' worth of accuracy. In object-oriented programming languages, type conversion allows programs to treat objects of one type as one of their ancestor types to simplify interacting with them.

There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. The most common form of explicit type conversion is known as casting. Explicit type conversion can also be achieved with separately defined conversion routines such as an overloaded object constructor.

Each programming language has its own rules on how types can be converted. In general, both objects and fundamental data types can be converted.

Implicit type conversion

Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler. Some languages allow, or even require, compilers to provide coercion.

In a mixed-type expression, data of one or more subtypes can be converted to a supertype as needed at runtime so that the program will run correctly. For example, the following is legal C language code:

double  d;
long    l;
int     i; 

if (d > i)      d = i;
if (i > l)      l = i;
if (d == l)     d *= 2;

Although d, l and i belong to different data types, they will be automatically converted to equal data types each time a comparison or assignment is executed. This behavior should be used with caution, as unintended consequences can arise. Data can be lost when floating-point representations are converted to integral representations as the fractional components of the floating-point values will be truncated (rounded down). Conversely, converting from an integral representation to a floating-point one can also lose precision, since the floating-point type may be unable to represent the integer exactly (for example, float might be an IEEE 754 single precision type, which cannot represent the integer 16777217 exactly, while a 32-bit integer type can). This can lead to situations such as storing the same integer value into two variables of type integer and type real which return false if compared for equality.

Explicit type conversion

Explicit type conversion is a type conversion which is explicitly defined within a program (instead of being done by a compiler for implicit type conversion).

Using Casting

double da = 5.5;
double db = 5.5;
int result = static_cast<int>(da) + static_cast<int>(db);
//Result would be equal to 10 instead of 11.

There are several kinds of explicit conversion.

checked
Before the conversion is performed, a runtime check is done to see if the destination type can hold the source value. If not, an error condition is raised.
unchecked
No check is performed. If the destination type cannot hold the source value, the result is undefined.
bit pattern
The raw bit representation of the source is copied verbatim, and it is re-interpreted according to the destination type. This can also be achieved via aliasing.

In object-oriented programming languages, objects can also be downcasted : a reference of a base class is casted to one of its derived classes.

Using Overloaded object constructor

class Myclass {
public:
double myD;
Myclass(double d) { myD = d; }
}
Myclass obj = Myclass(5.5);
double d = obj; //here is the explicit type conversion
//Result would be d = 5.5

See also