This expression uses old style casts - How to avoid??

In C/C++ we often use a regular type of type casting i.e (expected_type) value.

This type of type casting is applicable in C only. You can use the same type in C++ also but it will show a warning This expression uses old style casts. If you want to over come this warning you need to use the new style of type casting and this is applicable only in C++. Let us see how can we over come this warning.!

New style of type casting

The new style of type casting comes with 4 types, they are

  1. static_cast
  2. dynamic_cast
  3. const_cast
  4. reinterpret_cast

1.static_cast

It is used as a simple type of cast which we often use (implicit conversion), byte to word, int to float, etc.
static_cast<float>(variable_name)

2.dynamic_cast 

It works only when source type is polymorphism. We can type cast any pointer or reference to any polymorphism type, also we can say it converts a pointer or reference within a hierarchy
dynamic_cast<expected_type *>(Source_Var_Name)

3.const_cast

You can use this when you want to strips away the const requirement.

4. reinterpret_cast

It is used when low level reinterpreting a bit pattern. It should be used with a bit pattern alignment order caution in your mind.

This is how you can overcome the warning "This expression uses old style casts"


Cheers!!!!
-> Let Us Embed <-