3

Is there an actual difference between a const_cast and c style cast (ObjectType) ?

jmasterx
  • 52,639
  • 96
  • 311
  • 557

6 Answers6

11

A const_cast conveys specific information about the intent behind the cast that a C cast cannot.

If you accidentally try to use a const_cast for purposes other than adding or removing const or volatile, the compiler will help you with an error message.

Also, const_cast is searchable, unlike a C-style cast.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
4

A const_cast can only add or remove const-ness (or volatile-ness, though this is much less common).

A C-style cast can do the same as any of the "new" casts, except for a dynamic_cast (and it can do a few things none of them can do, though it's not really relevant here).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • @DavidRodríguez-dribeas: Being overly pedantic is almost always good for an up-vote or two (though sometimes only from others who are equally pedantic... :-) – Jerry Coffin Jun 19 '12 at 16:37
2

C-style cast in C++ attempts a static cast, a reinterpret cast, a const cast, or a combination of those.

It is recommended to avoid C casts mainly because...

  • reinterpret casts and const casts are used seldomly enough that it's good to emphasize what you're doing,
  • in other cases, when you want a static cast, writing it explicitly gives you additional compile-time checks compared to C casts.
Kos
  • 70,399
  • 25
  • 169
  • 233
  • `C` casts are yet a bit more powerful than the combination of the others. There are some corner cases where the only option is C-style casts (upcast to a private base, for example), although it can be argued that you should not write that code anyway... – David Rodríguez - dribeas Jun 19 '12 at 16:34
  • @DavidRodríguez-dribeas , that's interesting but I can't reproduce this; http://ideone.com/38Nyb – Kos Jun 19 '12 at 16:37
  • @Kos: Though it's not guaranteed, `reinterpret_cast` will usually work with single inheritance. With multiple inheritance, it'll usually work for one base, but fail for all the others (only *one* of the base classes can reside at the beginning of the derived object). – Jerry Coffin Jun 19 '12 at 16:40
  • @Kos: The difference is that a C style cast in that case is equivalent to a `static_cast` where the access specifier is ignored. Consider `struct D : A, B, private C {} d;` the output of `(void*)(C*)&d` and `(void*)reinterpret_cast(&d)` will differ. In the case of the C cast the pointer will refer to the actual `C` subobject, while in the case of `reinterpret_cast` it will (most probably, not guaranteed by the standard) refer to the location of the `D` object (or the first base) – David Rodríguez - dribeas Jun 19 '12 at 16:47
1

Same action. A C-style cast can cast away the const all the same.

The reason for const_cast is that it can serve as a searchable red flag, something to search for and carefully review/punish the guilty. The idea is that C++ is much more type-tight that C. So deliberate violations of the type system (such as violating const correctness), if not impossible, are easy to spot.

Making such violations of the type safety completely impossible would break too much backwards compatibility.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
1

const_cast can modify only the const-ness (or volatile-ness) of the argument, not it's basic type. So

 const T *tc = f();
 volatile T *tv = g();

 U *ua = const_cast<U*>(tc); //error
 U *ub = const_cast<U*>(tv); //error

 U *ub = (U*)(tc); //okay
 U *ub = (U*)(tv); //okay

So c-style cast modifies cv-qualified T* to U* without any problem.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

A const_cast is more restricted and won't let you do anything other than change const-ness. That makes it safer i.e. less accident-prone.

In addition it's easier to search for.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622