-1

We can use const_cast to pass const data argument to a function whose parameter is a non-const.

int fun(int* ptr)
{
    return (*ptr + 10);
}
int main(void)
{
    int val = 10;
    const int *ptr = &val;
    int *ptr1 = const_cast <int *>(ptr);
    cout << fun(ptr1);
    return 0;
}
Output:
20

But, we can achieve the casting in the following way also,

int fun(int* ptr)
{
    return (*ptr + 10);
}
int main(void)
{
    int val = 10;
    const int *ptr = &val;
    int *ptr1 = (int *)ptr;
    cout << fun(ptr1);
    return 0;
}

Output: 20

Then, what is the need for using const_cast in this particular scenario? Is there any advantage of using const_cast only in this particular scenario?

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • There's an advantage besides code clarity and safety: it's greppable. It's much harder to find all places where someone casts away constness using C-style casts. – molbdnilo Jan 30 '14 at 11:38
  • `(int*)` is the same as `const_cast` in this case. – Simple Jan 30 '14 at 12:46

2 Answers2

3

Because when you specify const_cast you explicitly tell that you wish to remove constness, while old-style cast allows you to cast anything to anything. See https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts

user3159253
  • 16,836
  • 3
  • 30
  • 56
  • can't able to access the mentioned link – user1099327 Jan 30 '14 at 11:36
  • Well, - "Trust me", - the character of Arnold Schwarzenegger in "Commando" movie. Actually C++-style casts were introduced as a replacement for old C-style casts. As mentioned above they are more explicit, they are easily searchable across the whole code, they literally strike reviewer's eye etc. If you have no explicit reasons to use old style casts it's better avoid them because they can hide a lot of programming mistakes and potentially dangerous operations. – user3159253 Jan 30 '14 at 12:14
0

The evil C-style cast will do just about any conversion that's remotely possible. By using that, you give up any hope of the type system catching mistakes in your code.

The C++ casts restrict the types of conversions that are possible, reducing the scope for accidentally casting to the wrong thing.

For example, this cast will compile, but give some kind of undefined behaviour if you try to use the pointer:

double * bad = (double*)ptr;

while this cast will fail to compile, since const_cast can't do type conversions:

double * less_bad = const_cast<double*>(ptr);

The casts work (more or less) as follows:

  • static_cast allows you to undo "safe" conversions; for example, converting a pointer to a base class to a pointer to a derived class, or void* to a typed pointer. It doesn't allow conversions that don't make sense under the type system.
  • reinterpret_cast allows wonkier conversions, such as converting pointers to integers or unrelated pointer types. Use it with care, if you need abandon the type system to do something funky.
  • const_cast allows you to remove const and volatile qualifiers, while not allowing type conversions.
  • dynamic_cast allows conversion between pointers or references to polymorphic types, with a run-time check that the conversion is valid. This is the only "safe" cast, as it cannot give a wrongly-typed result.
  • The evil C cast allows anything that static_cast, reinterpret_cast and const_cast can do, and more besides. The syntax is hard to spot, or to search for, so use this if you want to hide the true horror of your code and drive maintenance programmers into insanity.
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644