C++ Casting Operators Explained With Simple Example

Type casting is the method to convert variable (or expression) of a type to another type. Casting operators are special operators which are used to convert variable of one data type to another data type. Type casting can be done in two ways:

Implicit casting is being done automatically where values are copied to another compatible type. Eg: int to long int or float etc.

Explicit casting is done specifically by the programmer. In C++ there are four types of casting operator available.

(more…)
C++ Casting Operators Explained With Simple Example Read More

dynamic_cast Casting Operator Explained With Simple Example

This is one of the most important casting operators. The “dynamic_cast” performs a run-time type casting which also checks the type casting validity. If type casting is done to compatible type then it succeeds else it will throw “bad_cast” exception.

Normal syntax to do dynamic_cast is as follows:

dynamic_cast <target-type> (expr)

where target-type and expr must be of type pointer or reference.

(more…)
dynamic_cast Casting Operator Explained With Simple Example Read More