Passing pointer of class type

This is a discussion on Passing pointer of class type within the C++ Programming forums, part of the General Programming Boards category; ok i was thinking about making some (simple) code to see whether two rectangles (not actual ones--only the numbers like ...

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    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:
    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*
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    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.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yeah, that's just what I needed (encouragement included)...I'm going to sleep and I'll remember it tomorrow morning when I think how boring school is going to be...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "what im wondering is if it is possible to do it without passing in the height,width, and coordinates directly"

    Yes, and that's the way you should do it.

    bool isOverlapping(const Rect& r2) const

    If you are going to include the function in your class, you only need to pass in one parameter because the members of the object for which you are calling the function on can be referenced directly in the body of the function e.g

    Rect r1, r2;

    if(r1.isOverlapping(r2))
    {...}

    bool isOverlapping(const Rect& r) const
    {
    if(y_coord > r.y_coord){...}
    }
    Last edited by 7stud; 04-30-2003 at 10:53 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    By the way, the direct member selection operator(.) has higher precedence than the dereference operator(*), so this doesn't work:

    *r1.getMember()

    You would have to write it like this:

    (*r1).getMember()

    but since that notation is so cumbersome, you would use the indirect member selection operator(->) instead:

    r1->getMember()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21