Pointer to member function

Started by
21 comments, last by iMalc 14 years, 5 months ago
I did like this, inside the window procedure, because the windows message queue does not get any WM_SIZE message specificly.

case WM_SIZE:
PostMessage(p_hwnd, p_msg, p_wparam, p_lparam);
reinterpret_cast< Window* > (&hwnd) -> pollEvents();
break;

And pollEvents then examines the message queue for the window handle that is inside my Window class.
Advertisement
Remember what reinterpret_cast<SomeType *> means. It means "quiet compiler, I know what I'm doing here. Treat this block of bits like a pointer to SomeType".

My question is, do you know what you are doing here? The HWND passed to a WNDPROC is almost certainly not a pointer to a Window, let alone the address of that variable.

Quote:Original post by rikardo
I did like this, inside the window procedure, because the windows message queue does not get any WM_SIZE message specificly.

case WM_SIZE:
PostMessage(p_hwnd, p_msg, p_wparam, p_lparam);
reinterpret_cast< Window* > (&hwnd) -> pollEvents();
break;

And pollEvents then examines the message queue for the window handle that is inside my Window class.
That'll crash the first time you try to access a member variable of your Window class. After all, which instance of Window do you think that's casting to?

I don't think you've read rip-off's second like I said, so I'll link to it again: read this to understand how to make it work.

If there's something in that link that you don't understand, come back here with your question, but don't just keep trying random things until it compiles...
Ok, so its of no help that HWND actually is a pointer? And the Windows class wraps around just that address?
No. HWND is an opaque window handle. If it uses a pointer internally, that's not for you to know or use.

Really, if you're not using the GWL_USERDATA field of your window's associated data to hold your wrapper class instance, You're Doing It Wrong. Have you read that article yet? :)
Yeah I have, but this is the single row of code I need exactly that this works. And yes the router is the correct solution.
What makes you think reinterpret_casting a HWND to a Window * will work/is working? The value of the HWND is specified by Windows.
I think so since my Window class wraps the HWND, I just thought that the compiler would somehow find the correct window class since they share memory.
How does it wrap it? If you mean you have a HWND member in Window, that doesn't count.

For casting to work, you'd have to have a valid Window instance allocated in memory at whatever the value of the HWND is. This is basically impossible, since Windows is going to give you whatever value it feels like, whether a small integer, or a full 32-bit width one. Treating the value as a memory location isn't going to work.

Have you actually tried stepping over the reinterpret_cast< Window* > (&hwnd) -> pollEvents(); call in a debugger? What is the value of this in pollEvents? Is it the correct, valid Window instance?
Yes it is a member of the class.

Maybe Im lucky then? Because what I do is first is sending a WM_SIZE message and the related information to the queue of the HWND. And after that I make the cast and call pollEvents.

Now, pollEvents examines the message queue of the hwnd and it updates other members in the Window class correctly...

This topic is closed to new replies.

Advertisement