More reinterpret or static cast issues

C

Christopher

Last time I posted about reinterpret cast I was told I could use a
static cast in that case. Well here I am again in the same situation.
I try to static cast and the compiler complains "static_cast cannot
convert from DisplayMode * to LPARAM"

What am I doing wrong?

I am making a call to a Windows function (although this question is
about casting):
INT_PTR DialogBoxParam(
HINSTANCE hInstance,
LPCTSTR lpTemplateName,
HWND hWndParent,
DLGPROC lpDialogFunc,
LPARAM dwInitParam
);

The documentation suggets that if I want to pass a data structure to
my dialog that I am creating, that I should pass a pointer to my data
structure as the 5th argument.

Ok, so that is exactly what I try do do:

DialogBoxParam(instance, MAKEINTRESOURCE(IDD_GRAPHICS_CONFG,m_hwnd,
DlgProc, static_cast<LPARAM>(&m_displayMode));

I realize there a a bunch of windows types in here, but we are only
concerned with the 5th argument.
I am passing the address of a member of my class which is of type
DisplayMode. DisplayMode is a class I created myself.

LPARAM when searching down the typdef tree resolves to a __w64,
whatever that is. The docs claim it to be a "word", I assume it is
just a 64 bit word which is equivalent to a 64bit integer value(i
think).

So, why does my static_cast fail and must I use reinterpret_cast here
instead?
The book I am using just does a C-style cast of course, which I was
under the impression is equivalent to a static cast. Is that
incorrect?
 
S

SG

Last time I posted about reinterpret cast I was told I could use a
static cast in that case. Well here I am again in the same situation.

This was before we knew that you're trying to convert a pointer to an
integer and vice versa.
I try to static cast and the compiler complains "static_cast cannot
convert from DisplayMode * to LPARAM"
[...]
LPARAM when searching down the typdef tree resolves to a __w64,
whatever that is. The docs claim it to be a "word", I assume it is
just a 64 bit word which is equivalent to a 64bit integer value(i
think).
So, why does my static_cast fail and must I use reinterpret_cast here
instead?

Language rules. If you want to convert a pointer to an integer you
need reinterpret_cast and make sure that the integer type is big
enough to represent pointers. With another reinterpret_cast you can
convert this integer back to the original pointer. Still, it's
important that the pointer type is exactly the same.
The book I am using just does a C-style cast of course, which I was
under the impression is equivalent to a static cast. Is that
incorrect?

That's incorrect.

Cheers!
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top