Casting operators in C++: dynamic_cast, const_cast, static_cast, reinterpret_cast operator

Casting operators are used for controling casting in C++.

dynamic_cast operator: It is used for doing a run time cast to check the validity of a cast. It is used to cast pointers and references from one type to another.

Syntax of dynamic_cast:

dynamic_cast<target_type> (expression)

const_cast operator: It is used to control the const and volatile in a cast.

Syntax of const_cast:

const_cast<type> (expression)

static_cast operator: It is used to do a cast which is not polymorphic in nature.

Syntax of static_cast:

static_cast<type> (expression)

reinterpret_cast operator: It is used to change one type to another. For Example , to change a double to pointer.

Syntax of reinterpret_cast:

reinterpret_cast<type> (expression)

Leave a Reply