Expression processing

I've been trying to understand how C++ works through expressions, and built a small program to play with them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include"stdio.h"
#include"stdlib.h"

class Object{
	public:
	
	int value;
	
	Object(){value=0;}
	Object(int a){value=a;}
	
	Object operator+(Object);
};

Object Object::operator+(Object rhs){
	printf("i have value %d, it has value %d\n",value,rhs.value);
	return Object(value+rhs.value);
}

int main(){
	
	Object o1(1);
	Object o2(2);
	Object o3(4);
	Object o4(8);
	Object o5(16);

	o1+(o2+o3)+(o4+o5);
	
	return 0;
}


When I run this code, the program outputs

i have value 8, it has value 16
i have value 2, it has value 4
i have value 1, it has value 6
i have value 7, it has value 24


How come it didn't output

i have value 2, it has value 4
i have value 1, it has value 6
i have value 8, it has value 16
i have value 7, it has value 24

instead?

I'm using GCC to compile.
The compiler resolves multiple term expressions according to the heirarchy of the operators. In this case, the parens are higher precedence than the addition. The only thing I don't understand are why the first two lines aren't swapped, since I'd have figured it would do the first parens first, as they associate left to right.
That's what I'm saying.
Topic archived. No new replies allowed.