# Operator function: I'm not following the flow.

• 08-22-2011
tabl3six
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; }```
• 08-22-2011
tabstop
Just the same way the compiler does
Code:

`a = 3 + 7 + 11;`
You can only add two numbers at a time.
• 08-22-2011
tabl3six
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);}`
• 08-22-2011
laserlight
Yes. Incidentally, your member operator+ should be declared const since it does not change the observable state of the current object.
• 08-22-2011
tabl3six
Quote:

Originally Posted by laserlight
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);}`
• 08-22-2011
laserlight
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.
• 08-22-2011
tabl3six
so the final statement should be:
Code:

`Point operator+(const Point &pt) const {return const add(pt);}`
• 08-22-2011
laserlight
No. That would likely be a syntax error. Declaring add as const means:
Code:

`Point add(const Point &pt) const;`
• 08-22-2011
tabstop
No const inside the curlicues.
• 08-22-2011
tabl3six
I think I understand thanks for the help.