April 29, 2013

Casting in C++

As I most of my day time code in C, I used C-style casts in the beginning. The application was mixture of C and C++ (C++ API for C static library), therefore casts were needed (void pointer to data returned).

I corrected entire code, to replace with proper C++ casts. Because be aware of how C casts are interpreted in C++ language, they combine const_cast, static_cast and reinterpret_cast. One which works is used. Here are some disadvantages: hard to locate them, no compiler check, the ambiguity => not explicit, no dynamic runtime check (sometimes useful but costy - RTTI).

There are 4 casting operators  in C++: static_cast, const_cast, dynamic_cast, reinterpret_cast.

Small guide to casting in C++ [justcheckingonall.wordpress.com]
It gives an insight about C++ casting,