Suppose I define this structure:
struct Point {
double x, y;
};
How can I overload the +
operator so that, declared,
Here is how I would do it.
struct Point {
double x, y;
struct Point& operator+=(const Point& rhs) { x += rhs.x; y += rhs.y; return *this; }
struct Point& operator+=(const double& k) { x += k; y += k; return *this; }
};
Point operator+(Point lhs, const Point& rhs) { return lhs += rhs; }
Point operator+(Point lhs, const double k) { return lhs += k; }
Point operator+(const double k, Point rhs) { return rhs += k; }