| • Science | • People | • Locations | • Timeline |
| Contents |
In computer science, type conversion or typecasting refers to changing an entity of one datatype into another. A cast, or explicit type conversion, is a special programming instuction which specifies what data type to treat a variable as (or an intermediate calculation result) in a given expression.
Each programming language has its own rules on how types can be casted. In general, both objects and fundamental data types can be casted. Casting will ignore "extra" information (but never adds information to the type being casted).
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.
There are two common casting styles, each outlined below.
This style of casting is used in C and Java. It follows the form:
(type)expressionSeveral 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 + secondVariablemay not be clear, while the same cast using C++-style casting allows more clarity:
type(firstVariable + secondVariable)or
type(firstVariable) + secondVariableLater in the evolution of C++, the following more explicit casts were added to the language to clarify the programmer's intent even further:
static_castStatic casts converts type-compatible values. For instance the following:
double myDouble = 3.0; int myInt = static_castconverts the double-precision floating point value myDouble (3.0) to the corresponding integral value (3). Static casts can be dangerous:
Static casts on pointers or references don't verify that the pointed-to object is type-compatible to the new type.
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_castDynamic casts on pointers return a null pointer if cast value is type incompatible. Dynamic casts on a reference throw a type exception.
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_castThe reinterpret cast is the most notorious one in C++. It allows the reinterpretation of the raw bit pattern of the value to be cast, disregarding the type system completely. For example, it allows the casting of an arbitrary integer to a pointer to an object:
MyClass * pclass = reinterpret_castOpinions were divided when these verbose casts were introduced into the language. DetractorsThe term detractor is defined as a person that belittles the worth of another person or cause. The following terms are used interchageably with detractor slanderer. See slander libeler. See libel cynic mudslinger defamer. See defamation''. argued the new syntax was 'ugly', while supporters claimed that since casting is such an 'ugly' activity to begin with, it should be highlit with an 'ugly' syntax to alert programmersIn computing, a programmer is someone who does computer programming and develops computer software. A programmer can be one who develops and maintains software on a large mainframe system or one who develops software primarily for use on personal computer. Another perceived advantage is the ease with which verbose casts can be located in source codeSource code (commonly just source or code is any series of statements written in some human-readable computer programming language. In modern programming languages, the source code which constitutes a software program is usually in several text files, but using programming tools like grepgrep is a command line utility originally written for use with the Unix operating system. The name comes from a command in the Unix text editor ed that takes the form g re p meaning "search g lobally for matches to the r egular e xpression re and p rint l.
Computer science