I have never taken a written test, but I am often asked with questions from the written test, especially the questions about ++ and -- operators. In fact, these questions should be understood from the basic principle, however, in the written test, there is still a simple method to calculate this problem. Summary:
1) in calculation, replace the prefix operand (++ I) in a scope with the final value of I and the suffix operator with the original value.
2) In printf, cout, and other stack-dependent methods, replace the prefix operator (++ I) in a scope with the final value of I. For suffix operators, analysis is performed based on the order of the inbound stack.
1: int a ;
2: //------------------------------------------------------------------------------------
3: // The following part does not include the prefix operator. First, it is followed by a ++ inbound stack 1, A = 2, and then the first a ++ inbound stack 2.
4: a = 1;
5: cout<<"a "<<a++<<" "<<a++<<endl; //2,1
6: cout<<"a "<<a<<endl; //3
7: a = 1;
8: printf("a %d %d\n",a++,a++); //2,1
9: printf("a %d\n",a); //3
10:
11: //------------------------------------------------------------------------------------
12: // The following part contains the prefix operator. The final value of A is 3. First, the value of ++ A is 3, and then the value of ++ A is 3.
13: a = 1;
14: cout<<"a "<<++a<<" "<<++a<<endl; //3,3
15: cout<<"b "<<a<<endl; //3
16: a = 1;
17: printf("a %d %d\n",++a,++a); //3,3
18: printf("a %d\n",a); //3
19:
20: //------------------------------------------------------------------------------------
21: // The following part contains the prefix operator. The final value of A is 3. The first is that ++ A is 3 in the stack, after a = 2, the inbound stack of A ++ is 2.
22: a = 1;
23: cout<<"a "<<a++<<" "<<++a<<endl; //2,3
24: cout<<"a "<<a<<endl; //3
25: a = 1;
26: printf("a %d %d\n",a++,++a); //2,3
27: printf("a %d\n",a); //3
28:
29: //------------------------------------------------------------------------------------
30: // The following part contains the prefix operator. The final value of A is 3. First, the value of a ++ is 1, after a = 2, the inbound stack of ++ A is 3.
31: a = 1;
32: cout<<"a "<<++a<<" "<<a++<<endl; //3,1
33: cout<<"a "<<a<<endl; //3
34: a = 1;
35: printf("a %d %d\n",++a,a++); //3,1
36: printf("a %d\n",a); //3
37:
38: int b;
39: a = 1;
40: b = 1;
41: // both the left and right operators are B ++. If no prefix operator exists, the left and right operands are replaced with 1, A = 1 + 1 = 2.
42: a = (b++)+(b++);
43: printf("a = (b++)+(b++) %d %d\n",a,b);//2,3
44: a = 1;
45: b = 1;
46: // contains the prefix operator. Replace the prefix operator with 3, A = 3 + 3 = 6.
47: a = (++b)+(++b);
48: printf("a = (++b)+(++b) %d %d\n",a,b);//6,3
49: a = 1;
50: b = 1;
51: // because the prefix operator is included, the part of the prefix operator is replaced with 3, and the former value is included in the suffix operator. A = 3 + 1 = 4
52: a = (++b)+(b++);
53: printf("a = (++b)+(b++) %d %d\n",a,b);//4,3
54:
55: a = 1;
56: b = 1;
57: // because the prefix operator is included, the prefix operator is replaced with 6, and the suffix operator is still the original value 1, A = 6 + 6 + 6 + 1 + 1 = 20
58: a = (++b)+(++b)+(++b)+(b++)+(b++);
59: printf(" a = (++b)+(++b)+(++b)+(b--) %d %d\n",a,b); //20,6
For the overloaded ++ and -- operators, the situation is similar. For example, pay attention to the 22nd lines of code:
1: class Example{
2: public:
3: Example(int i,int j) { _x = i; _y = j;}
4: // when there is no virtual parameter in the prefix form (++ I) Reload, * This is returned through reference, that is, the value after the change is returned.
5: const Example& Example::operator++ () {
6: ++_x;
7: ++_y;
8: return *this;
9: }
10: // when the suffix form (I ++) is overloaded, there is a virtual parameter of the int type, and a copy of the original state is returned.
11: const Example Example::operator++ (int) {
12: Example tmp(*this);
13: ++_x;
14: ++_y;
15: return tmp;
16: }
17: int _x, _y;
18: };
19: Example ei(1,2);
20: cout <"EI" <(EI ++ ). _ x <"" <EI. _ y <Endl; // at the time of 1, 2 EI ++, a copy of the original EI status is returned. At this time, the original EI status has been updated.
21: cout <"EI" <(EI ). _ x <"" <EI. _ y <Endl; // The status of 2, 3 EI has been updated, _ x = 2, _ y = 3
22:Cout <"EI" <(++ EI ). _ x <"" <EI. _ y <Endl; // 3. The first is EI. _ y indicates that the EI has not been updated at this time. The number of incoming Stacks is 3, and then the incoming stacks (++ EI ). _ x, the EI has been updated, and the inbound stack is 3
23: cout <"EI" <(EI ). _ x <"" <EI. _ y <Endl; // at the time of 3, 4 + + EI, the EI has been updated, _ x = 3, _ y = 4;