# 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. ## 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 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 new_pt;
new_pt.x = x + pt.x;
new_pt.y = y + pt.y;
return new_pt;
}```

2. Just the same way the compiler does
Code:
`a = 3 + 7 + 11;`
You can only add two numbers at a time.

3. 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. Yes. Incidentally, your member operator+ should be declared const since it does not change the observable state of the current object.

5. 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);}`

6. 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
{
}```
Of course, add should be declared const as well.

7. so the final statement should be:
Code:
`Point operator+(const Point &pt) const {return const add(pt);}`

8. No. That would likely be a syntax error. Declaring add as const means:
Code:
`Point add(const Point &pt) const;`

9. No const inside the curlicues.

10. I think I understand thanks for the help.