C++ – Type Casting

Typecasting in C++ allows us to perform conversion between types.

C-Style Cast
This was carried forward from C- language. Compiler does not perform type checking and hence this type of cast can result in run time errors/ undefined behavior. Therefore It is best to avoid them in the program. For example,  consider the following.

Example 1

Example 2

C++ introduced four new casting operators – static_cast, dynamic_cast, const_cast and reinterpret_cast.

static_cast
static_cast addresses many of the pitfalls associated with the C-style cast. It performs thorough type checking at compile time and throws an error when there is a type mismatch. The earlier examples can therefore be changed to the following. Both the programs will throw compile time errors.

Example 1

Example 2

static_cast does not work well with inheritance. When we have a pointer to a base class that may or may not be pointing to an object of its derived class and we try to explicitly convert this pointer to a pointer of the derived class, static_cast will happily do the conversion for us. This is not desirable as the base class pointer may as well be pointing to an object of the base class.


Output:

b is not nullptr

dynamic_cast
dynamic_cast addresses the pitfalls associated with compile time type checking. It performs type checking during run-time and is able to catch the above incorrect type conversion. dynamic_cast returns a null pointer when the cast performed is illegal.


Output:

b is nullptr

There are two other casting operators in C++: const_cast and reinterpret_cast. const_cast allows us to remove the ‘const’ and ‘volatile’ properties of types. It works with pointers and references.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s