In the previous chapter, we have already touched on the overloading of the incrementing operator. At that time, we did not distinguish the difference between the incrementing first and the latter, normally, we do not see the differences between ++ a and a ++, but they do have obvious differences directly.
First look at the following code:
C ++ code
# Include <iostream> Using namespace std;
Int main () { Int a = 0; ++ (++ A); // correct. (++ a) returns the left value. (A ++) ++; // error. (a ++) the returned value is not the left value. System ("pause "); }
|
|
In the Code (a ++) ++ compilation error, the error "++" requires the left value is returned, which is caused by the difference between the pre-increment and post-increment, why?
The reason is mainly caused by the definition of the increment operator in C ++.
The main differences between them are as follows:
1. During the operation, the object is first incrementally modified, and then the name of the object (actually the object reference) is returned.
Increment (increment) Operation. Return object references in operator overload functions.
2. During the operation, the value of the original object is returned first, and then the incremental operation of the object is called
Post-increment (incremental) Operations. In the operator overload function, use the value return method to write (this is also the reason for the previous (a ++) ++ error, (a ++) returned is not a reference, it cannot be used as the left value to continue the ++ operation outside the number Extension). The internal implementation of the overload function must create an object for temporary storage of the original object value, the function returns the temporary object.
So how can we distinguish between the previously incrementing operator overload function and the post-incrementing operator overload function when writing an operator overload function?
The method is to add an int identifier to the parameters of the Post-incrementing operator overload function and mark it as the post-incrementing operator overload function.
For details, see the following example (for example, non-member mode and for example, member mode ):
C ++ code
// Example 1
// Program Author: Guan Ning // Site: www.cndev-lab.com // All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream> Using namespace std;
Class Test { Public: Test (int a = 0) { Test: a =; } Friend Test & operator ++ (Test &); Friend Test operator ++ (Test &, int ); Public: Int; }; Test & operator ++ (Test & temp) // increment first { Temp. a ++; Return temp; } Test operator ++ (Test & temp, int) // returns an ascending value. int serves only to differentiate the value. In fact, it does not actually work. { Test rtemp (temp); // The copy constructor is called to copy objects. Temp. a ++; Return rtemp; } Int main () { Test a (100 ); ++ (++ ); Cout <a. a <endl; Cout <"Status of the value of the temporary storage object in ascending mode:" <(a ++ ). a <endl; // here is the position where the original object value is returned first after the incremental operation Cout <a. a <endl; (A ++) ++; Cout <. a <endl; // because the value returns the status after incrementing, (a ++) ++ only performs one incrementing operation on a, and the operation is 104 instead of 105. System ("pause "); }
|
|
C ++ code
// Example 2
// Program Author: Guan Ning // Site: www.cndev-lab.com // All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream> Using namespace std;
Class Test { Public: Test (int a = 0) { Test: a =; } Test & operator ++ (); Test operator ++ (int ); Public: Int; }; Test & Test: operator ++ () // ascending { This |
|