When reading C ++ primerCode:
Istream_iterator <Int> In_iter (CIN );//Read ints From CINIstream_iterator <Int> EOF;//Istream "end" iterator//Read until end of file, storing what read in VECWhile(In_iter! =EOF ){//Increament advances the stream to the nest Value//Dereferent reads next value from the istreamVEC. push_back (*In_iter ++ );}
For *In_iter ++,We know that ++ has a higher priority than *, so it is equivalent to writing*(In_iter ++), which means to perform an auto-increment operation on the istream_iterator object to move the iterator forward in the stream. However, the result of the post-incrementing expression is the original value of the iterator. The auto-increment effect is to move the iterator to the next value in the stream, but the return value refers to the iterator with the previous value. This iterator is obtained by unreferencing it.
To fully understand this section, we need to have a deep understanding of the auto-incrementing operators.
The auto-incrementing operators include the plus sign (++) and the plus sign (A ++). Let's look at the following code:
# Include <iostream> Using Namespace STD; Int Main (){ Int A =5 ; Int B = ++ A; cout < " B = " <B < " \ T " < " A = " <A < Endl; Return 0 ;}
Running result:
B = 7 A = 7
# Include <iostream> Using Namespace STD; Int Main (){ Int A = 5 ; Int B = (a ++) ++ ; Cout < " B = " <B < " \ T " < " A = " <A < Endl; Return 0 ;}
Running result:
Compilation error: Error c2105: '++' needs L-Value
We know that the auto-increment operator ++ is the combination direction from right to left (VC ++ 6.0), so ++ A is also writing ++ (++). According to the combination of ++, A ++ is definitely wrong. As to whether errors will occur in (a ++) ++ and ++ A ++, you will know after analyzing a ++ and ++.
Differences between a ++ and ++:
(1) In the operation process, the object is first incrementally modified, and then the former incremental operation (actually the object reference) is returned.++. Return object references in operator overload functions.
(2) In the operation process, the original object value is returned first, and then the object incremental operation is called post incremental operation.A ++. You can use the return method in the operator overload function. The internal implementation of the overload function must create an object for temporarily storing the original object value. The function returns the temporary object.
Now let's analyze the above question: for (A ++) ++, the first operation (A ++), but (A ++) does not return a reference, instead, it is a copy of the original A value. At this time, the copy is no longer a variable or a constant, so it cannot be used as the left value to continue the ++ operation outside the brackets. As for ++ A ++, that is, ++ (A ++), it cannot be compiled for the same reason.
Auto-increment operator ++ overload:
How do I distinguish between a previously incrementing operator overload function and a post-incrementing operator overload function when writing an operator overload function? The method is to add an int identifier to the parameter of the Post-incrementing operator overload function and mark it as the post-incrementing operator overload function.
# Include <iostream> Using Namespace STD; Class Test { Public : Test ( Int A = 0 ) {Test: = A;} friend Test & Operator ++ (Test & ); FriendConstTest Operator ++ (Test &, Int ); Public : Int A ;}; Test & Operator ++ (Test & Val) // Ascending {Val. ++ ; Return Val ;}ConstTest Operator ++ (Test & Val, Int ) // Post-increment. Int only plays a differentiated role here, which has no practical significance. {Test temp (VAL ); // The copy constructor is called to copy objects. Val. A ++ ; Return Temp ;} Int Main () {test ( 5 ); ++ Test; cout < " Test. A = " <Test. A < Endl; cout < " Status of the value of the temporary storage object in ascending mode: " <(Test ++). A < Endl; cout < " Test. A = " <Test. A < Endl; test test1 ( 5 ); (Test1 ++) ++ ; Cout < " Test1.a = " <Test1.a < Endl; cout < Before Status of the value of the temporary storage object in ascending mode: " <(++ Test1). A < Endl;
Cout<"Test1.a ="<Test1.a <Endl;Return 0;}
Running result:
Test. A = 7
Status of the value of the temporary storage object in ascending mode: 7
Test. A = 8
Test1.a = 6
Status of the value of the temporary storage object in ascending order: 7
Test1.a = 7
The above code is in a non-member format.ProgramCommon methods and skillsWe recommend that you reload the operator function as a member function for all unary operators..
# Include <iostream> Using Namespace STD; Class Test { Public : Test ( Int A = 0 ) {Test: = A;} Test &Operator ++ ();ConstTest Operator ++ ( Int ); Public : Int A ;}; Test & Test :: Operator ++ () // Ascending { This -> A ++; Return * This ;}ConstTest test :: Operator ++ ( Int ) // Post-increment. Int only plays a differentiated role here, which has no practical significance. {Test temp ( * This ); // The copy constructor is called to copy objects. This -> A ++ ; Return Temp ;} Int Main () {test ( 5 ); ++ Test; cout < " Test. A = " <Test. A < Endl; cout < " Status of the value of the temporary storage object in ascending mode: " <(Test ++). A <Endl; cout < " Test. A = " <Test. A < Endl; test test1 ( 5 ); (Test1 ++) ++ ; Cout < " Test1.a = " <Test1.a < Endl; cout < " Status of the value of the temporary storage object in ascending order: " <(++ Test1). A < Endl; cout < " Test1.a = " <Test1.a < Endl; Return 0 ;}
Running result:
Test. A = 7
Status of the value of the temporary storage object in ascending mode: 7
Test. A = 8
Test1.a = 6
Status of the value of the temporary storage object in ascending order: 7
Test1.a = 7
Note: Why (test1 ++) ++ can be compiled, but the above mentioned (A ++) ++ cannot be compiled, this is because after (test1 ++), if the temporary variable is temp, then temp = test1 (the original test1 value) and then temp ++ is used. A ++, while temp member A is a variable, so you can perform auto-increment operations.
Note that the const marked in red is added after me, but when I add it, the running result above will go wrong, because temp is the const object, in the future, we cannot add ++ automatically. In my opinion, we should add const to avoid the appearance of (test1 ++) ++.
Finally, let's try again,
Test&Operator++();
ConstTestOperator++ (Int);
Why is a reference returned, and a const returned.
Refer:
(1) 30 days to master the essence of C ++
(2) http://www.cnblogs.com/hazir/archive/2012/04/16/2451933.html#2356352
(3) More effective C ++ 4.2 item M6: differences between prefix and suffix formats of increment and Decrement Operators