Operator overloading in C ++

If you are overloaded - like operator- (), it should be used to the left of the object, however, overload (), like operator () (), is used to the right of the object. How do we know which operator should be used on the left and which on the right?

+6
c ++ operator-overloading
source share
2 answers

Look at the priority chart. This will tell you the direction that the operator is linking (linking). Please note that some operators have several forms with different meanings, for example, binary and unary - . In such cases, you may have several overloads, for example:

 T operator-() 

and

 T operator-(const T &o) 

The compiler chooses the right one based on the syntactic interpretation of the statement.

See also this helpful set of recommendations .

+3
source share

Most unary operators can only be placed on a specific side of their operand. For two special cases, ++ and -- see this FAQ .

+1
source share

All Articles