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:
reinterpret_cast
work, What is the magic(the internal implementation) that allows reinterpret_cast to work?reinterpret_cast
? As far as i know, it doesn't guarantee of safe casting, So what precaution to take while using reinterpret_cast?
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_cast
normally 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_cast
from IPHdr
to TCPHdr
(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:
class XXX {
public:
operator YYY() { return whatever; }
};
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 explicit
qualifier 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.
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