May be the question will be a little bit noobie, but can anybody advice me some books/sites/articles about how and when it's better to use typecasting in C++ style and when it would be better to prefer C-style? I've read a few books (Shildt, Eckel) and haven't found good material on this theme though I think it's one of the strongest C++ abilities.
There's not much to it. Use the features of the language you are coding in, subject to these being supported by any compiler you might use.
If you're coding in C++, you should use C++ typecasting, as it can prevent bugs. Generally speaking, if your design is correct you should rarely need to use reinterpret_cast
or const_cast
- static_cast
should do most of what you need (or dynamic_cast
, depending on your app).
The basic rule is
- In C, use C-style casting:
int i = (int) x;
- In C++, use new style casting:
int i = static_cast<int>(x);
The reasons are:
- Casting is any ugly thing to do, so it should look ugly.
- There are different reasons why you want to cast something, and the new style casts let you make your intentions clear.
- Some things, like
dynamic_cast
, cannot be done with C-style casting.
I think a good advice is to use C++ style casts whenever possible.
They make the intention of the cast more explicit, are much more easier to search for, and you tend to think more about a cast compared to when using the old-style casts.
C++ casting allows you to specify exactly what you expect the cast to do, thus allowing more errors to be caught at compile time, or correctly handled at runtime (depending on which cast you use.)
For example, if you use dynamic_cast and the cast fails, it returns NULL from the cast which you can then check in a subsequent call. If a C style cast fails, your program crashes.
Read about what each of the casts does and it should make sense. The trick is learning which you need ;)
Airsource answered your question perfectly, let me add to it. Never use any C features when there is a C++ feature for it unless there is no way around it (or you are Hacking, in the true meaning of the word). There are security issues with some of the C features that was fixed with C++, e.g. malloc.
Generally in C++ you should use C++ style casting only.
However, there are situations where you can excuse using C-style casting, for example, when you cast values of other types than pointer and reference:
int g1() { SomeEnum e = getEnum(); return (int)e; }
Here you could have used static_cast<int>( e ) - but here (int)e is not dangerous, and it's shorter. You could use also int(e) instead, however for the builtin types this is equivalent to C-style casting. Consider also:
cout << (int)'a';
where casting is also the only way to force using 'int' type and display the code of 'a', not 'a' itself. This is just the same safe and shorter than this:
cout << static_cast<int>( 'a' );
Note, however, that C-style casting is dangerous for pointer/reference types because it has no limitations about what can be done (you can cast everything to everything).
A similar question was already asked. You might find some useful answers there.
From my understanding, the C style casting, there is no type-checking, whereas the C++ style static_cast has built in type checking. As a result, the C++ method is definitely recommended unless you intentionally want to avoid type-checking (in which case use reinterpret_cast).