4.11.3. Explicit Conversions
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.
Named Casts
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 ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.