August 2012
Intermediate to advanced
976 pages
30h 17m
English
Sometimes we want to explicitly force an object to be converted to a different type. For example, we might want to use floating-point division in the following code:
int i, j;double slope = i/j;
To do so, we’d need a way to explicitly convert i and/or j to double. We use a cast to request an explicit conversion.
Warning
Although necessary at times, casts are inherently dangerous constructs.
A named cast has the following form:
cast-name<type>(expression);
where type is the target type of the conversion, and expression is the value to be cast. If type is a reference, then the result is an lvalue. The ...
Read now
Unlock full access