Casting without runtime checks

Most casts are performed using the static_cast operator, and this can be used to convert pointers to related pointer types as well as converting between numeric types. There are no runtime checks performed so you should be certain that the conversion is acceptable:

    double pi = 3.1415;     int pi_whole = static_cast<int>(pi);

Here a double is converted to an int, which means that the fractional part is discarded. Normally the compiler would issue a warning that data is lost, but the static_cast operator shows that this is your intention and hence the warning is not given.

The operator is often used to convert void* pointers to a typed pointer. In the following code the unsafe_d function assumes that the parameter ...

Get Beginning C++ Programming 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.