https://en.cppreference.com/w/cpp/language/reinterpret_cast
An expression of integral, enumeration, pointer, or pointer-to-member type can be converted to its own type. The resulting value is the same as the value of expression. (since C++11)
Why should I be interested in converting something to its own type? This reads to me as I would want to convert int to int.
Please correct me.
Because maybe you don't know if it is "its own type". Maybe you're in a template function, where you're calling some function whose return value you require to be reinterpret_castable to a template parameter T. So it's OK for that function to return a value of type T.
For a more useful example, let's say you're given some object of a type T. And you call a function on that that's supposed to return a pointer of some sort. And you want to cast this pointer to a char* and do some low-level messing around, perhaps to pass it to memcpy or something.
Well, what happens if the function returned a char*? That is, it returned a string. You need reinterpret_cast<char*> to still work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With