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

reinterpret_cast Casting Operator Explained With Simple Example

This is one of the most complex and dangerous casting operators available in C++. The “reinterpret_cast” operator can convert any type of variable to fundamentally different type. This cast operator can convert an integer to a pointer and so on. This cast operator can also convert variable into totally incompatible type too.

Normal syntax to do reinterpret_cast is as follows:

reinterpret_cast <target-type> (expr)

target-type is the target of the cast whereas expr is being cast into the new target-type.

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