Windows Class

Started by
9 comments, last by force_of_will 21 years, 8 months ago
Hello Guys Well My Problem: I''m Doing A Class To Make My Windows But The Problem Is That I Want To Make The Windows Procedure Inside That Class the Problem is in this line of code when i assing the WndcProc of My Functio To My winclassex Structure this ->WindowClass.lpfnWndProc = this ->WndProc; //Adress To The Windows Procedure the two erros it gives me are this c:\Documents and Settings\Arsénio\My Documents\Visual Studio Projects\WinMain\OGLWindow.cpp(16): error C2440: ''='' : cannot convert from ''LRESULT (__stdcall COGLWindow::* )(HWND,UINT,WPARAM,LPARAM)'' to ''WNDPROC'' c:\Documents and Settings\Arsénio\My Documents\Visual Studio Projects\WinMain\OGLWindow.cpp(16): error C2475: ''COGLWindow::WndProc'' : forming a pointer-to-member requires explicit use of the address-of operator (''&'') and a qualified name IF It isn''t possible to do the WndProc inside a class please let me know if it is possible and you now how please HELP ME
Advertisement
You have to make your WndProc a static class member. This static method can be just a wrapper that calls a non-static wndproc in a class associated with the HWND it''s given. See this article by Oluseyi for an example of how to do it.
quote:Original post by force_of_will
this ->WindowClass.lpfnWndProc = this ->WndProc; //Adress To The Windows Procedure

Needs to be static (_stdcall). There''s a ton of threads and even an article on the topic. Use the Search tool ("window proc class member" is a good search string) and check the Articles & Resources section.
THANKS
NOW I CAN ASSIGN IT

JUST ONE MORE LITLLE THING CAN YOU EXPLAIN EHY IT NEEDS TO BE STATIC


Because you can''t pass the address of a member function for a specific instance of a class, unless it is a static function.
And the reason that you can't use a normal (ie: non-static) class method is because such methods are defined using the __thiscall modifier (in MSVC anyway, in other compilers, it may be slightly different but the idea is the same). __thiscall is essentially a way to refer to class methods and implicitly adds the this pointer as an argument to the function.

The this pointer has to be passed so the method will know which instance of your class to work with.

This is a problem because the callback for the window procedure has a set of parameters that, quite obviously, does not include a pointer to whatever class you happen to have created.

So what you have to do is declare your method as static. Because of how static methods work (if you don't know how they work, read up on them), they have no need for a this pointer and are thus entirely allowable as window procedure functions.

Hope that made sense...

-Auron

[edited by - Auron on July 24, 2002 11:55:05 PM]
quote:Original post by smitty1276
Because you can''t pass the address of a member function for a specific instance of a class, unless it is a static function.

sure you can. look up pointers to member functions in your favorite c++ reference.

---
Come to #directxdev IRC channel on AfterNET
if he uses vc6 he can''t.


and he can''t here anyways because the interface of a memberfunction is different than the one of a WNDPROC defined function. they have different parameters and a different rule how to pass them.

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Thanks I Will Work Right Away On My Class
quote:Original post by davepermen
if he uses vc6 he can''t.

niyaw was making a general statement. It''s hard to address issues like this comprehensively.

You can take the address of any type of function (because a function - even a class method - is merely a bit of code situated at a particular relocatable address). You can not, however, assign any function address to any function pointer: the pointer and function pointed to must match in terms of argument list. Class members in most (all?) C++ implementations have the this pointer as the first parameter and therefore utilize a different calling convention than "plain" functions to indicate this. Since the pointer in question is retrieved from code over which you have no control, the only solution is a static class member.

You can even use non-static class members as callbacks. The only caveat is that the calling code must know the class type and so forth.

Basically what you and others said, all put together.

This topic is closed to new replies.

Advertisement