The friends who can come in must be as concerned as I am count = count++ I'm not sure about the results , I hope you get something after watching it .

Let's take a look at the code first
public class CountTest { public static void main(String[]args) { int count =
0; // initialization count by 0 for(int i=1;i<=100;i++) { // ergodic 100 second count = count++; }
System.out.println("count = "+count); // output count } }
In the code ,count Is initialized to 0, And then I went through it 100 second count=count++ , The output answer must be equal to 100 ah . Isn't that the way of post self increasing . In fact that was not the case. , The answer is 0

I have come to the conclusion that the answer is equal to 100 One of the reasons for this is that ,count++ It's after ++, It must be first count The value of is assigned to count And then in the count+1 Well ( FALSE ), congratulations , You're in the same hole as me . Because that's what I think .

I've really thought about a lot of wrong ways , Still unable to get the right answer , When I'm obsessed with solving problems , I'm starting to wonder what I'm thinking about count++ Understanding of . Isn't it count First assign to the left value and then +1 Is that right ( FALSE )
?   yes , At last I found out that it was wrong here .

Here's what I did to you count = count++ The exploration of .

1. after ++ and assignment = Priority of .

I used to study C Language and C++ When I was young , Every time j = i++ When I was young , That's how it is understood , First of all i The value of is assigned to j then i=i+1. Now I find out
++ The operation priority of is higher than that of (), explain ++ The priority of operation is very high , And assignment = The operation priority of is very low , How low is it , than || All low , What I wanted to do first is ++, Isn't that right . In absolute high priority, it seems that = cannot withstand a single blow ? So is it first i+1 And then it's assigned to j Is that right , That's what it is j=++i Did you? , Where is this j
=i++. Laugh to death , At this time, I found that I only found half of the reasons , about count =
count++ I'm still in the dark . therefore , I think of a red book ,《C++ Object oriented programming ( Second Edition )》 Operator overloading in Tan Haoqiang ++ Is that right . you 're right , That's it !

2.C++ Can operator overloading get to this ?

If it's true for count
=count++ It bothers a lot of people ( Like me ), We'll turn to C++ The second part of object oriented programming 138 The example of page ( below ). of course , There are also many examples of overloading the post auto increment operator on the Internet , You can also go and have a look .
Time Time ::operator ++ (int ) { Time temp(*this); sec++; if(sec>=60) {
sec-=60; ++minute; } return temp; }
It doesn't matter if you don't understand , Note that it looks as if the code is going to return a Time class , And then there's one in the code Time  temp(*this), stay sec++, And then back temp
. Oh, Hoo , Use a temporary variable to save the current object itself , then sec+1 It's over , Finally, the saved object itself is returned , The reason to keep the object itself is because sec+1 Can lead to Time The object itself changes the returned value . I seem to understand a little bit . It uses a temporary variable to store the object , The object is then returned in the .

be relative to count = count ++ In my words , That is to say, one is used temp Saved count Initial value of , then count+1 It's over , But assignment to count What's important is that temp .
That is to say, it is added first and then assigned , But not exactly , The assigned value is temporarily saved count.

3. about j = i++ Examples of

Now I'll make an analogy j = i++ The process of solving the problem .
public class CountTest { public static void main(String[]args) { int i = 0;
int j = 0; j = i++; System.out.println("i = "+i); // The output is 1 System.out.println("j
= "+j); // The output is 0 } }
i,j Initialization is 0 , First of all, there is one int temp Variables are retained i(=0) Value of , then i =i+1 , last temp Assigned to j. It's gone .
j Becomes the initial value i,i become i+1 It's over ,

j = i++ Analogy to count = count++, That's it temp Saved count Initial value of , then count+1 It's over , last temp Assigned to count.
Right value count The value of is in ++ Previously saved to temp, And then right-handed count It was meant to be +1, namely count=1 It's over , But it has to be done in the end count= Assignment operation , hold temp Preserved count The initial value is assigned to the left value . It's like count It's changed , But it didn't change
;

Therefore, in the actual programming, don't make this small detail , Cause the program to run unsatisfactory oh .

That's what I'm interested in count = count++ Thinking on the issue , Hope it works for you .

The above only applies to java environment . stay c/c++ In the environment , Different compilers result in their own way of handling the code , It will lead to different running results .

 

 

 

Technology