HWND 的类型转换运算符重载返回垃圾而不是预期的成员值
我定义了一个要用于构建窗口的类。其中一个字段是 hWnd,当调用成员函数 create() 时,创建的窗口的 HWND 就存储在那里。我重载了 (HWND) 转换以返回该值:
operator HWND() { return(hWnd); }
当我尝试为我创建的第一个主窗口创建子窗口时,我的程序开始崩溃,并且我跟踪它到类型转换返回的奇数值。我定义了一个典型的 getter 函数 getHwnd(),它工作正常,但类型转换只是返回垃圾。我缺少什么吗?
类定义:
class WindowBuilder
{
public:
WindowBuilder(FullWindow &fullWindow);
operator HWND() { return(hWnd); }
void SetCaption(char const * caption) { windowName = caption; }
void SetMenu(int resourceId);
void SetRender(RECT rect, HWND parent);
void SetButton(HWND parent);
void Create();
void Show(int nCmdShow = SW_SHOWNORMAL);
HWND getHwnd () { return(hWnd); }
protected:
FullWindow & window;
HWND hWnd;
char const * windowName;
DWORD style;
int x;
int y;
int width;
int height;
HWND hWndParent;
HMENU hMenu;
void * data;
};
调用示例:
FullWindow renderWindowClass("STATIC", GlobalInstance, WndProc);
renderWindow = new WindowBuilder(renderWindowClass);
renderWindow->SetRender(rect,mainWindow->getHwnd()); // used to be (HWND)mainWindow
renderWindow->Create();
renderWindow->Show(CmdShow);
/*RenderWindow = ::CreateWindow("STATIC", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
DEFAULT_BUTTON_WIDTH, 0, rect.right-rect.left-DEFAULT_BUTTON_WIDTH,
rect.bottom - rect.top, Window, NULL, hInstance, NULL);*/
I defined a class that I want to use for building a window. One of the fields is hWnd and when the member function create() is called the HWND to the created window is stored there. I overloaded the (HWND) cast to return that value:
operator HWND() { return(hWnd); }
My program started crashing when I attempted to create child windows to the first, main window I created and I tracked it down to an odd value being returned by the typecast. I defined a typical getter function, getHwnd(), that works fine but the typecast just returns trash. Is there something that I'm missing?
The class definition:
class WindowBuilder
{
public:
WindowBuilder(FullWindow &fullWindow);
operator HWND() { return(hWnd); }
void SetCaption(char const * caption) { windowName = caption; }
void SetMenu(int resourceId);
void SetRender(RECT rect, HWND parent);
void SetButton(HWND parent);
void Create();
void Show(int nCmdShow = SW_SHOWNORMAL);
HWND getHwnd () { return(hWnd); }
protected:
FullWindow & window;
HWND hWnd;
char const * windowName;
DWORD style;
int x;
int y;
int width;
int height;
HWND hWndParent;
HMENU hMenu;
void * data;
};
Example of calling:
FullWindow renderWindowClass("STATIC", GlobalInstance, WndProc);
renderWindow = new WindowBuilder(renderWindowClass);
renderWindow->SetRender(rect,mainWindow->getHwnd()); // used to be (HWND)mainWindow
renderWindow->Create();
renderWindow->Show(CmdShow);
/*RenderWindow = ::CreateWindow("STATIC", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
DEFAULT_BUTTON_WIDTH, 0, rect.right-rect.left-DEFAULT_BUTTON_WIDTH,
rect.bottom - rect.top, Window, NULL, hInstance, NULL);*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有显示
mainWindow
的定义,但根据您对 -> 的使用,我假设它是一个指向 WindowBuilder 的指针。因此,当您执行(HWND)mainWindow
时,您是将指针值强制转换为 HWND,而不是调用强制转换运算符。要调用强制转换运算符,您需要执行类似(HWND)(*mainWindow)
的操作。话虽如此,我不得不说,如果您不以这种方式使用强制转换重载,您将拥有一个更清晰的设计。由于在代码中很难发现的自动转换,它们可能会导致微妙的错误。使用显式的
getHwnd()
成员更加清晰和可靠。You don't show the definition of
mainWindow
, but based on your use of ->, I would assume that it is a pointer to a WindowBuilder. So when you do(HWND)mainWindow
, you are casting the pointer value to an HWND rather than invoking your cast operator. To invoke your cast operator, you would need to do something like(HWND)(*mainWindow)
.Having said that, I feel obliged to say that you would have a cleaner design if you do not use cast overloads in this way. They can lead to subtle bugs due to automatic casts that can be hard to spot in the code. Using an explicit
getHwnd()
member is much more clear and reliable.