Passing pointer of class type
ok i was thinking about making some (simple) code to see whether two rectangles (not actual ones--only the numbers like coords and dimensions exist) are overlapping
i created a "Rect" class and then i got down to passing in the values in each rectangle.
what im wondering is if it is possible to do it without passing in the height,width, and coordinates directly:
Code:
if (isOverlapping(&Rect1,&Rect2))
instead of something like this:
Code:
if (isOverlapping(Rect1.getHeight(),Rect1.getWidth(),Rect1.getXCoord(),Rect1.getYCoord());
i tried the first method and in the function:
Code:
if (((*r1.getRectYCoord+*r1.getRectHeight)>=(*r2.getRectYCoord))&&((*r1.getRectYCoord+*r1.Height)<=(*r2.getRectYCoord+*r2.getRectHeight)))
return true;
//trying to use a pointer the the class type
and I get a compiler time error:
Quote:
36 main.cpp
request for member `getRectYCoord' in `r1', which is of non-aggregate type `Rect *'
...
im kinda tired right now so i might be doing something entirely stupid...but i dont think so...*yawns*
Re: Passing pointer of class type
When accessing members of a struct or a class through a pointer, you need to do either:
Code:
obj->Member();
// or
(*obj).Member();
You were on the right track, you just need to add the parenthesis in there. What would simplify things even more, is having your function take references to rects, instead of pointers to rects. Like so:
Code:
bool isOverlapping(const RECT& r1, const RECT& r2) { /* Definition */ }
By having your function take references instead of pointers, you don't have to worry about pointer syntax inside your function. You can access members of the RECT structure via the '.' operator. obj.Member();
Hope this helps.