C++ RECT,将自定义RECT结构传递给需要RECT的函数
我有一个自定义的rect函数。自定义rect如下所示:C++ RECT,将自定义RECT结构传递给需要RECT的函数,c++,rect,C++,Rect,我有一个自定义的rect函数。自定义rect如下所示: typedef struct tagRECTEx{ // long left; // long top; // long right; // long bottom; RECT dimensions; int width()) { return dimensions.right-dimensions.left; } int height(){ return dimensions.bottom - dimensions.to
typedef struct tagRECTEx{
// long left;
// long top;
// long right;
// long bottom;
RECT dimensions;
int width()) {
return dimensions.right-dimensions.left;
}
int height(){
return dimensions.bottom - dimensions.top;
}
} RectEx;
现在,让我们说:
RECT windowrect;
windowrect = GetWindowRect(hWnd,&windowrect);
我希望是这样:
RectEx windowrectex;
windowrect = GetWindowRect(hWnd,&windowrectex);
....
现在它不会编译,因为它不能将rectex转换为tagRECT,好的,我明白了
所以在过去的几天里,我一直在搜索自定义强制转换和覆盖操作符
我甚至在尝试实现以下内容:
GetWindowRect(hWnd, (RectEx)&windowrectex);
但不管我在做什么,我就是想不出如何让它发挥作用
我想使用我自己的rect结构,因为它会自动为我获取rect的宽度和高度,而不是执行rect.right-rect.left等操作
如果你需要更多的信息,请告诉我
谢谢只需从
RectEx
内部传递RECT
RectEx windowrectex;
windowrect = GetWindowRect(hWnd,&windowrectex.dimensions);
或者,您可以使RectEx
从RECT
继承并删除维度
或者,添加一个转换运算符,运算符RECT*()const
,如dbasic建议的那样。然后你会使用:
windowrect = GetWindowRect(hWnd,windowrectex);
由于GetWindowRect
将LPRECT
作为第二个参数,因此无法传递RectEx
RectEx windowrectex;
windowrect = GetWindowRect(hWnd,&windowrectex.dimensions);
要使用RectEx
,可以按如下方式重载类型转换操作符
operator LPRECT () const
{
return &dimensions;
}
但是,由于不希望进行类型转换,因此不建议重载类型转换。只有在确定的情况下才能这样做。为什么不编写一些非成员函数,使用RECT参数并返回宽度/高度?并非所有内容都必须是成员函数。