The reinterpret_cast as we know can cast any pointer type to any another pointer type. The question I want to ask regarding this cast operator are:
- How does
reinterpret_castwork, What is the magic(the internal implementation) that allows reinterpret_cast to work? - How to ensure safety when using
reinterpret_cast? As far as i know, it doesn’t guarantee of safe casting, So what precaution to take while usingreinterpret_cast? - What is the practical usage of this operator. I have not really encountered it in my professional programing experience, wherein I could’nt get around without using this operator.Any practical examples apart from usual int* to char* will be highly helpful and appreciated.
One other Question regarding casting operators in general:
Casting operators(static_cast, dynamic_cast, const_cast, reinterpret_cast) are all called Operators i.e is to the best of my understanding, So is it correct statement to make that casting operators cannot be overloaded unlike most other operators (I am aware not all operators can be overloaded and I am aware of which can’t be(except the Q I am asking, Please refrain flaming me on that) Just I had this doubt that since they are operators, what does the standard say about these?
reinterpret_castnormally just means (at least try to) treat what you find at this address as if it was the type I’ve specified. The standard defines little enough about what it does that it could be different from that, but it rarely (if ever) really is.reinterpret_castfromIPHdrtoTCPHdr(or whatever names you happen to have used). The compiler won’t (again, normally) do much though — any safety is up to you to implement and enforce.For your final question: you can overload casting for a class:
This can be used for conversions in general though — whether done by a static_cast, C-style cast, or even an implicit conversion. C++0x allows you to add an
explicitqualifier so it won’t be used for implicit conversions, but there’s still no way to differentiate between a static_cast and a C-style cast though.