In C++, can I simply cast a pointer to a DWORD?
MyClass * thing;
DWORD myPtr = (DWORD)thing;
Would that work?
http://en.cppreference.com/w/cpp/language/explicit_cast
Read that, understand that, avoid C-style casts because they hide a lot.
Doing so may be able to be done, but would make no sense, for example DWORD is 4 bytes and a pointer (these days) is 8.
reinterpret_cast<DWORD&>(myPtr);
Should work, but it may be undefined or truncate, if anything will work that will!
BTW, reinterpret_cast
is the C++ way of saying "Trust me my dear compiler, I know what I'm doing" - it attempts to interpret the bits (0s and 1s) of one thing as another, regardless of how much sense that makes.
A legitimate use though is the famous 1/sqrt hack ;)
You undoubtedly can do it.
Whether it would work will depend on the environment and what you want it to do.
On 32-bit Windows1 (the most common place to see
DWORD
) it'll normally be fine. On a 64-bit Windows (where you also seeDWORD
, but not nearly as much) it generally won't.