RECT , passing a custom rect struct into a function that is requireing RECT

StackOverflow https://stackoverflow.com/questions/20158903

  •  04-08-2022
  •  | 
  •  

문제

ok so I have a custom rect function. The custom rect is as follows:

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;

Now instead of using lets say:

RECT windowrect;
windowrect = GetWindowRect(hWnd,&windowrect);

I would like it to be this instead:

RectEx windowrectex;
windowrect = GetWindowRect(hWnd,&windowrectex);

....

Now that won't compile because it cannot convert rectex to tagRECT, ok I get this I understand this.

So for the last few days i've been searching on custom casting and overriding operators.

I'm even trying to implement something like:

GetWindowRect(hWnd, (RectEx)&windowrectex);

but not matter what i'm doing I just can't figure out how to get this to work.

I want to use my own rect struct as it will autoamticly get the width and height of the rect for me instead of doing rect.right-rect.left and etc.

If you need anymore info about this or anything please let me know.

thank you

도움이 되었습니까?

해결책

Since GetWindowRect takes LPRECT as the second parameter, there is no way to pass RectEx.

What you can do to use RectEx, you can overload typecasting operator as follows

operator LPRECT () const 
{
   return &dimensions;
}

However, overloading typecasting is not recommended due to undesired type conversion. Only do when you are sure about it.

다른 팁

Just pass the RECT from inside your RectEx.

RectEx windowrectex;
windowrect = GetWindowRect(hWnd,&windowrectex.dimensions);

Alternatively, you can make RectEx inherit from RECT and remove dimensions.

Alternatively, add a conversion operator, operator RECT*() const like dbasic suggests. Then you would use:

windowrect = GetWindowRect(hWnd,windowrectex);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top