Overloading Functions

Started by
15 comments, last by DevFred 15 years, 1 month ago
1st, here is my Code the class Declaration class mWindow { //Set's up Varibles ... ... //tobe Overloaded Functions static bool Close() {return true;} //Runs shutdown Functions bool Init() {return true;}; static bool Render() {return true;} } ok, the three Functions Init() / Close() and Render() are supposed to be overloaded, however ... The Close is called in the WndProc() message loop on WM_DESTROY. and only seems to like it if its static. Here is the Class derived from mWindow, which overloads the Functions class WinSetup : public mWindow { private: public: //Overloaded functions WinSetup(); bool Init(); bool Close(); bool Render(); }; Now the Functions Should to my understanding overload ... and the Init() works perfectly, the problem is with the Render Function is not being called through WndProc(); do i need to overload WndProc? LRESULT CALLBACK mWindow::WndProc(HWND hWnd , UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_DESTROY: Close(); PostQuitMessage(WM_QUIT); break; case WM_CREATE: break; case WM_PAINT: Render(); break; default: return DefWindowProc(hWnd, Msg, wParam, lParam); } return 0; }
Advertisement
If you have base class functionality that you want to replace with derived class behavior then you should define the functions as virtual.
You can't use static methods polymorphically. Remember, the only methods that are overridden are virtual methods, and they cannot be static.

The problem is that your WndProc has no context, it does not know which Window instance to manipulate. This is why you had to make the functions static for them to compile.

The solution is to either only allow a single Window instance, or use some other method to obtain a Window instance inside the WndProc. The former is pretty easy, make a global Window instance (or pointer to a derived instance) and use that. It is inelegant however.

The latter is addressed by this article.
with my WndProc, how do i declare it to only use a Specified instance (i have m_hWnd defined as a Member Handle in my mWindow Class seems that this would be the easiest way, i did think that i needed to declare as virtual.


LRESULT CALLBACK mWindow::WndProc(HWND hWnd , UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
Close();
PostQuitMessage(WM_QUIT);
break;
case WM_CREATE:
break;
case WM_PAINT:
Render();
break;

default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}

as I am new to the Win32 API I am not Familiar with other Methods use with message handling
Go read my post in your last thread again.
ok, so what i need to do is :

class myClass
{
void msgHandle(); //Contains code(a)
static LRESULT CALLBACK WndProc(...);
}

//
m_wcex.lpfnWndProc = &msgHandle



Given the fact that won't even compile, that's a no.
SiCrane gave you the exact filler code you need in that other thread.
Ok, so I read you code, and I think I can understand some(:P) of it
at the moment i get the following errors here

class mWindow
{
public:
//Other code emmited
void WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
}
LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (LONG_PTR user_data = GetWindowLongPtr(hWnd, GWLP_USERDATA))
{
mWindow* this_window = reinterpret_cast<mWindow *>(user_data);
return this_window->WndProc(hWnd, Msg, wParam, lParam); // cannot return this
}
if (Msg == WM_NCCREATE)
{
LPCREATESTRUCT create_struct = reinterpret_cast<LPCREATESTRUCT>(lParam);
void * lpCreateParam = create_struct->lpCreateParams;
mWindow * this_window = reinterpret_cast<mWindow *>(lpCreateParam);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this_window));
return this_window->WndProc(hWnd, Msg, wParam, lParam); //Cannot return this
}

return DefWindowProc(hWnd, Msg, wParam, lParam);
}

1>c:\users\alex\documents\visual studio 2008\projects\win basics\win basics\mwindow.cpp(102) : error C2440: 'return' : cannot convert from 'void' to 'LRESULT'
1> Expressions of type void cannot be converted to other types
1>c:\users\alex\documents\visual studio 2008\projects\win basics\win basics\mwindow.cpp(110) : error C2440: 'type cast' : cannot convert from 'void' to 'LRESULT'
1> Expressions of type void cannot be converted to other types
You declared your WndProc function as a void return type. That's not right.

This topic is closed to new replies.

Advertisement