19.8 TYPE CASTING OPERATORS

We have seen basic type casting earlier. Now Standard C++ offers many new cast operators. Let us see them one by one.

19.8.1 static_cast operator

The static_cast operator has syntax as shown below.

static_cast < type-id > (expression)

This operator is used to convert between related types. For example, you can convert numerical types such as float to int, Pointers to derived class to pointer to base class, etc. Inadvertent use of type casting leads to serious errors. This casting checks certain cases for compatibility. However, it cannot be guaranteed that every conversion is meaningful. However, using this casting operator at least increases readability. Here we (the programmers) are at least aware of such casts. ...

Get Object Oriented Programming with C++, Second 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.