when a reinterpret_cast is applied to a class object, it returns a LONG_PTR. is it some sort of handle to a temporary object pointer stored somewhere in memory?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The question’s not so well worded. I can’t think of any manner of object for which
reinterpret_cast<LONG_PTR>(object)would work, assumingLONG_PTRis some pointer type (it’s not Standard C++), and object is a class instance (the C++ Standard considers variables of built in types to be objects too – that could work).You may be thinking of:
In this case,
1234is never a run-time “object” – not even a temporary int in memory – it’s just a number that the compiler converts to aLONG_PTRas if you’d usedstatic_cast<>. You can think of this as “give me aLONG_PTRwith this value”.You could reinterpret to a reference-to-object:
In this case, the pointer returned doesn’t necessarily refer to any object that your program has, or even to memory that your program can access. There is no temporary created there for your convenience (as per your question). You’re simply telling the compiler that
sizeof(LONG_PTR)bytes starting at the base address ofmy_objectought to contain aLONG_PTR. Hopefullymy_objector other code has set it somehow to point somewhere useful, or to a recognisable sentinel value likeNULL. Think of this as “give me a reference to theLONG_PTRbits already stored in the front ofmy_object, letting me treat them as aLONG_PTRand forget all aboutmy_object” – you’d better hope those bits/bytes are aligned in memory in a way your CPU can handle, or you could getSIGBUSor equivalent.You can also reinterpret a pointer-to-object to a
LONG_PTRIn this case, you know the returned value does point somewhere in your program, to valid memory, although if
sizeof(LONG_PTR)is greater thansizeof(my_object), then not all the memory it addresses is withinmy_object: any remainder could be from another variable, the stack, or again be inaccessible. Think “give me aLONG_PTRaimed atmy_object‘s memory, which should (or will if you write to it) hold the -value- that aLONG_PTRnormally addresses (perhaps a “LONG”, or perhapsLONG_PTRis a generic pointer of a particular bit-size?)”.Note these are all very different.
More generally,
reinterpret_castreturns whatever type you pass as a template argument, which may or may not be yourLONG_PTR, but there’s certainly nothing inherently inreinterpret_cast<>that relates to yourLONG_PTRtype.