4
struct Point2D {
float x;
float y;
};

Point2D operator+(Point2D lhs, Point2D rhs);
Point2D operator-(Point2D lhs, Point2D rhs);
Point2D operator*(Point2D lhs, Point2D rhs);
Point2D operator/(Point2D lhs, Point2D rhs);

Also, what is 'operator' and what is it's role?

Barry
  • 286,269
  • 29
  • 621
  • 977
Demadoggo
  • 81
  • 1
  • 1
  • 2
  • 1
    lhs => _left hand side_, rhs => _right hand side_. The left and right operands of the operator. –  Jan 29 '18 at 02:19
  • 2
    Check out one of the beginner books [here](https://stackoverflow.com/q/388242/2069064). They are good. – Barry Jan 29 '18 at 02:20
  • 3
    Possible duplicate of [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) –  Jan 29 '18 at 02:22
  • they are just names. you can use any valid name there. – apple apple Jan 29 '18 at 03:00

1 Answers1

6

lhs and rhs stand for Left Hand Side and Right Hand Side in this context.

They are being used as inputs to operator over loader functions. The Left Hand side is being compared to the Right Hand.

Operator Overloader functions change the way operator symbols work.

Point2D operator+(Point2D lhs, Point2D rhs);

The above code will add (hence the +) the lhs to the rhs. Since Point2D is not a primitive numerical data type, C++ doesn't know to add two of them together. You are required to tell the compiler how the + operator should be executed for the Point2D datatype.

Tyler Fricks
  • 91
  • 1
  • 8