mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-05 22:38:31 +00:00
c++: Add more explanation to the += overloading example
This commit is contained in:
parent
84cb0e8899
commit
696cbc66be
@ -553,10 +553,14 @@ Point Point::operator+(const Point& rhs) const
|
|||||||
return Point(x + rhs.x, y + rhs.y);
|
return Point(x + rhs.x, y + rhs.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// It's good practice to return a reference to the leftmost variable of
|
||||||
|
// an assignment. `(a += b) == c` will work this way.
|
||||||
Point& Point::operator+=(const Point& rhs)
|
Point& Point::operator+=(const Point& rhs)
|
||||||
{
|
{
|
||||||
x += rhs.x;
|
x += rhs.x;
|
||||||
y += rhs.y;
|
y += rhs.y;
|
||||||
|
|
||||||
|
// `this` is a pointer to the object, on which a method is called.
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user