What decides the operation sequence of a class Operator Overloading?

1
2
3
4
5
6
7
8
class test{
...
public:
...
  test operator+(const test &a)const;//#1
  const test & operator+=(const test &a);//#2
  std::ostream & operator<<(std::ostream &os, const test &a);//#3
}

#1, if I have a statement testa = testa + testb + testc + testd;, how would I know the sequence is left-to-right, or it's right-to-left?
testa.operator+(testb.operator+(testc.operator+(testd)));//right-to-left
((testa.operator+(testb)).operator+(testc)).operator+(testd);//left-to-right
Both works.

#2, similar to #1

#3, Due to the return type is on the left side of the argument list, the statement seems can only to the left-to-right version. If the return type is const test &a, it can be cout<<(fout<<(cout<<testd)), but it's obviously meaningless here.

So my question is the #1 version.
The choice between parsing a + b + c as (a + b) + c or as a + (b + c) is decided by the C++ language grammar, and is often explained as "associativity of operators"

See for example http://en.cppreference.com/w/cpp/language/operator_precedence : operators + and << associate left-to-right, and operator += right-to-left.
Evaluation depends on the operator precedence (usually left to right), then values are returned (right to left). Think of a stack data structure

http://lmgtfy.com/?q=C%2B%2B+operator+precedence
Topic archived. No new replies allowed.