views:

1684

answers:

9

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.

+6  A: 

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).

Airsource Ltd
+17  A: 

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.
James Curran
Another reason is "easier to search/grep" in a project, and thus, review each and every cast in case is amiss.
paercebal
Make that static_cast<int>(x);
Eclipse
Josh: It is; just that the poster didn't mark the static_cast<int> in a Markdown code thingy and the <int> got killed by the HTML sanitiser. :-)
Chris Jester-Young
You should be explicit about the reason you are casting - use static_cast for static primitive conversions, use reinterpret_cast for integer/pointer conversions, user dynamic_cast for downcasting, and use const_cast for removing const/volatile qualifiers.
Adam Rosenfield
+3  A: 

The only case where I can think of to use the C-style casts is in code which must be able to compile with C++ and C-compilers. Otherwise the C++ cast are always preferable. They are not only more safe, but much easier to search for if needed.

+1  A: 

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.

fhe
+1  A: 

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 ;)

Rob Prouse
If you have a bad C-style cast, you're lucky if the program crashes. There's no telling what could happen.
Fred Larson
Minor correction: `dynamic_cast` only returns NULL on error if you pass it a pointer. If you pass a reference or value, it throws a `bad_cast` exception.
Head Geek
Thanks Head Geek. I do very little C++ these days, so I am getting rusty ;)
Rob Prouse
+1  A: 

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.

WolfmanDragon
I don't know of any security problems with the malloc specification, as opposed to particular implementations. Could you expand on this?
KeithB
KeithB: Careless coding with malloc can result in integer overflow errors. e.g., malloc(length * sizeof (SomeType)); if length isn't checked to be less than `SIZE_T_MAX / sizeof (SomeType)`, the multiplication will have surprising results.
Chris Jester-Young
That makes sense, although its arguable that this is a problem with malloc itself, and if C++ deliberately fixed it, or it was just a coincidence.
KeithB
Don't know the why, just that they did. Sorry I can;t be more help on that
WolfmanDragon
A: 

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).

+1  A: 

A similar question was already asked. You might find some useful answers there.

Serge
A: 

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).