Jump to content
  • Advertisement
Sign in to follow this  
Andy474

Overloading Functions

This topic is 3417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
Advertisement
If you have base class functionality that you want to replace with derived class behavior then you should define the functions as virtual.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
ok, so what i need to do is :

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

//
m_wcex.lpfnWndProc = &msgHandle



Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!