Utility of coercion operator reinterpret_cast

1

What is the coercion operator reinterpret_cast ?

    
asked by anonymous 12.08.2018 / 17:40

1 answer

0

Do you know the cast of other languages, spatially from C? cast is just an indication to treat a data of one type as another type, you change a characteristic to be seen in that object. In some cases this will convert the value, because this value has another size and / or bit encoding. There are cases that do not need to change anything, you just need to do it this way to tell the compiler that you know what you're doing and it's exactly what you want, that you know more about it than the context.

In C ++ it is preferred, although the C way still works, to use specific casts for each situation by semantically clarifying what one wants, and the code to demonstrate what operation is being done. There are some casts , such as: const_cast , static_cast and dynamic_cast .

The reinterpret_cast , as the name says, is just an indication to the compiler that the type to be considered there at that point in the code is different from the actual type of die. It indicates that you know the data is of another type than expected at that point and you know the data fits well where it will be used the way it is.

The compiler would normally bar the compilation giving error because it knows they are different types, but it does not know as much as you and can not automatically consider this ( some types it knows yes, for example it knows that short can be using where int is expected, or Cachorro can be used in several places where expects a Animal (assuming one inherits from the other).

The linked documentation has the description of the cases that works and is appropriate and use example.

    
12.08.2018 / 18:07