+ 2

Use case of reinterprete_cast in c++

There are four type cast... const, static, dynamic and reinterpret static cast convert relevant class pointers ;both way upcast and downcast as well.. dynamic cast convert relevant class pointers only downside (upside is implicit call used in polymorphism). type cast check internally for possibility of conversion.. const cast convert static constant members. Reinterpret cast convert relevant as well as non related class, but it might be dangerous to dereferencing the same... Can anyone tell me use case of reinterpret cast? what is the need to provide the same in c++?

20th Jul 2018, 1:38 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Réponses
+ 3
C++ its powerful for this also, it allow programmers to make unsafe things ... Anyway personally i think that reinterpret_cast its used in some case where low level conversion (to byte level i intend) its needed and not in every-day usage
20th Jul 2018, 1:50 PM
KrOW
KrOW - avatar
+ 2
Reinterpret cast is used to pass the binary data to another type, without any conversion. This is not that common and could be unsafe if used wrong. One example could be: Float types don't allow you to print the hex of how they are stored in memory. So you could reinterpret cast to an int type the sales size, then hex that. Why? well if you want do something implementation dependant to speed up the code. The game industry brought us quick inverse square root.
21st Jul 2018, 1:56 AM
Jared Bird
Jared Bird - avatar
+ 2
in binary file writing and reading but we can also use other casts to write and read to a file.
28th Jul 2018, 3:37 AM
Abdul Wahab Khan
Abdul Wahab Khan - avatar