Operator function: I'm not following the flow.

This is a discussion on Operator function: I'm not following the flow. within the C++ Programming forums, part of the General Programming Boards category; I'm a little confused on how the program reads the math operations for this line: Code: Point point4 = point1 ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    46

    Operator function: I'm not following the flow.

    I'm a little confused on how the program reads the math operations for this line:

    Code:
    Point point4 = point1 + point2 + point3
    It seems to me that the way the code is set up it can only add two point objects at a time.

    Code:
    Point operator+(const Point &pt) {return add(pt);}
    How does the operator function know how to add the sum of point1 and point2 to point3. Is this where the inline function
    Code:
    {return add(pt)}
    comes in?

    I know this is very basic to some. I'm having a hard time understanding this.

    Complete code is below:

    Code:
    //this program sets three Point objects to non-negative, then adds them creating
    //the 4th Point object.
    
    #include <iostream>
    using namespace std;
    
    class Point {
    private:             // Data members (private)
        int x, y;
    public:              // Constructors
        Point() {};
        Point(int new_x, int new_y) {set(new_x, new_y);}
        Point(const Point &src) {set(src.x, src.y);}
    
    // Operations
    
        Point add(const Point &pt);
        Point operator+(const Point &pt) {return add(pt);}
        
    // Other member functions
    
        void set(int new_x, int new_y);
        int get_x() const {return x;}
        int get_y() const {return y;}
    };
    
    int main() {
    
        Point point1(20, 20);
        Point point2(0, 5);
        Point point3(-10, 25);
        Point point4 = point1 + point2 + point3;
    
        cout << "The point is " << point4.get_x();
        cout << ", " << point4.get_y() << "." << endl;
    
        system("PAUSE");
        return 0;
    }
    
    void Point::set(int new_x, int new_y) {
        if (new_x < 0)
            new_x *= -1;
        if (new_y < 0)
            new_y *= -1;
        x = new_x;
        y = new_y;
    }
    
    Point Point::add(const Point &pt) {
        Point new_pt;
        new_pt.x = x + pt.x;
        new_pt.y = y + pt.y;
        return new_pt;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Just the same way the compiler does
    Code:
    a = 3 + 7 + 11;
    You can only add two numbers at a time.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Let me see if I'm understanding correctly.
    Code:
    Point point4 = point1 + point2 + point3;
    This has two function calls to:
    Code:
    Point operator+(const Point &pt) {return add(pt);}
    The first one I understand how that works. In the second call what would be "this object"? Would that be the new_pt from
    Code:
    {return add(pt);}

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Yes. Incidentally, your member operator+ should be declared const since it does not change the observable state of the current object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Quote Originally Posted by laserlight View Post
    Yes. Incidentally, your member operator+ should be declared const since it does not change the observable state of the current object.
    I thought it was
    Code:
    Point operator+(const Point &pt) {return add(pt);}

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by tabl3six
    I thought it was
    That declares the parameter as a const reference. I am talking about:
    Code:
    Point operator+(const Point &pt) const
    {
        return add(pt);
    }
    Of course, add should be declared const as well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    so the final statement should be:
    Code:
    Point operator+(const Point &pt) const {return const add(pt);}

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    No. That would likely be a syntax error. Declaring add as const means:
    Code:
    Point add(const Point &pt) const;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No const inside the curlicues.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    I think I understand thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional operator :? with function calls
    By dnj23 in forum C Programming
    Replies: 7
    Last Post: 08-10-2010, 01:16 PM
  2. What is the difference between operator and function?
    By chottachatri in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2008, 04:53 AM
  3. for_each function and operator()
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2007, 10:14 AM
  4. function/operator size??
    By studentc in forum C Programming
    Replies: 7
    Last Post: 05-24-2004, 02:02 PM
  5. operator function overloads
    By The WaRped OnE in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2001, 06:45 PM

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