C style casts

I've made many posts in the past with regards to C (on different forums) to have people comment on it saying I should use C style casts if I'm programming in C++. Well I wasnt, so I didnt care, but now I am, so I'm just curious about "standard" practices in C++ as I'm starting out.

Working with the windows GDI and using SelectObject, I can only see myself using casts to do something like
 
HBITMAP oldBmp=(HBITMAP)SelectObject(hdc,newBmp);


Does this apply to what they were telling me? Or is their another "better" way to do it?


Thank you for any feedback.
There are two aspects.

First, C has one only cast. C++ has many: static_cast, dynamic_cast, const_cast, reinterpret_cast. Each of them does a more precise purpose. You might build an entire house with a sledge-hammer, but having more purpose-built specialized tools is probably a good thing.

Second, you want to find all explicit casts from your code for some reason. The C-style casts are hard to spot, but the C++-style casts are easy to find automatically (grep/findstr/...).

Maintainability of the code is very important and the C++-style casts are good for it.
Makes sense, thank you.
The problem with C casts is you're never quite sure what they're doing. It'll silently do anything it takes to achieve the conversion.
Topic archived. No new replies allowed.