Pure virtual function call

I need help to overcome this problem:

Window base class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Window
{
protected:
    ...
    static LRESULT CALLBACK StaticWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
	virtual LRESULT WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) = 0;
public:
    ...
};

LRESULT CALLBACK Window::StaticWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	Window* pWindow;
  
	if(WM_NCCREATE == msg)
	{
		pWindow = reinterpret_cast<Window*>( ((LPCREATESTRUCT)lParam)->lpCreateParams );
		SetWindowLongPtr(hWnd, GWL_USERDATA, reinterpret_cast<LONG_PTR>( pWindow ));
	}
	else
	{
		pWindow = reinterpret_cast<Window*>( GetWindowLongPtr(hWnd,GWL_USERDATA) );
		if(nullptr == pWindow) 
		{
			return DefWindowProc(hWnd, msg, wParam, lParam);
		}
	}

	pWindow->hwnd = hWnd;
	return pWindow->WindowProc(hWnd, msg, wParam, lParam);
}


Window "child" class
1
2
3
4
5
6
7
8
9
10
11
12
13
class MainWindow : public Window
{
private:
public:
    ...
    LRESULT WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
};

// implementation of base pure virtual method
LRESULT MainWindow::WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    ...
}


And got error: "R6025 - pure virtual function call"

Any ideas how to do this?

Thanks for your time.
How are you passing the pointer to CreateWindow(Ex) in the first place?

Are you using reinterpret_cast to cast a MainWindow* to PVOID?

Andy
Last edited on
1
2
3
4
5
6
7
8
9
Window::Window(...)
{
...
hwnd = CreateWindow(
			className.c_str(), winTitle.c_str(), wndStyle,              
			center ? windowLeft : rect.x, center ? windowTop : rect.y, realWidth, realHeight,
			nullptr, nullptr, hinstance, this);
...
}
Last edited on
Ah ha!

You shouldn't call virtual functions from inside a constructor. The vtable isn't fully fixed up until the construction of the instance is complete.

Try reworking things so you use 2-phase construction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Window::Window(...) : hwnd(nullptr), ...
{
}

bool Window::Init() // or whatever makes sense to you
{
    ...

    hwnd = CreateWindow(
			className.c_str(), winTitle.c_str(), wndStyle,              
			center ? windowLeft : rect.x,
			center ? windowTop : rect.y,
			realWidth, realHeight,
			nullptr, nullptr, hinstance, this);
    ...
}


1
2
3
4
Window* wnd = new Window;
wnd->Init();

...


Andy
Last edited on
Thats it. Thanks.
Topic archived. No new replies allowed.