I am trying to learn C++ by coding a simple class to take an input of two doubles, a real and imaginary part, and perform operations on them. I have one section in particular,
cout << "((c1 += c2) += c3) = " << ((c1 += c2) += c3) << endl;
cout << "c1 = " << c1 << endl;
In this case, it correctly prints what the sum of c1, c2, and c3 is, but c1 only updates to the sum of c1 and c2. I'm wondering why that is the case.
First of all ((c1 += c2) += c3)
in this code first of all inner bracket will be executed which is (c1+=c2
this will give sum of c1 and c2 after which it will be added with c3 (c1+c2)+=c3
will add c3 to the sum.
Since c3 is added to the sum of c1 and c2 and not to the variable c3. Therefore c1 only have sum of c1 and c2 only.
If you want want c1 to have sum of c1 ,c2 and c3 as well then try this ((c1=(c1 += c2)) += c3)
Hope you got your answer.