Utility of the coercion operator reinterpret_cast

Asked

Viewed 77 times

1

What is the coercion operator for reinterpret_cast?

  • Now you can vote for everything on the site.

1 answer

0


Knows the cast of other languages, especially C? O cast is just an indication to treat a die of one type as another type, you change a feature to be seen on that object. In some cases this passes for converting the value, because this value has another size and/or bit encoding. There are cases that do not need to change anything, just need to do it this way to indicate to the compiler that you know what you are doing and it is exactly what you want, that you know more about the context than it.

In C++ it is preferred, although the way C still works, to use Casts specific to each situation making it clear semantically what is desired, and that the code demonstrates 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, it is only an indication to the compiler that the type to be considered there at that point of the code is different from the actual type of data. Indicates that you know that the die is of another type than expected at that point and know that the die fits well where it will be used the way it is.

The compiler would normally crash the build because it knows they are different types, but it doesn’t know as much as you do and can’t automatically consider this (some guys he knows yes, for example he knows that a short can be using where one expects a int, or a Cachorro can be used in several places where one expects a Animal (whereas one inherits from the other).

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.