C + + Specifies that the suffix form has an int type parameter, and when the function is called, the compiler passes a value of 0 as the int parameter to the function.
The prefix form of the increment means "add and then retrieve", and the suffix form means "fetch and then increment".
1#include"stdafx.h"2#include"assert.h"3 classA4 {5 Public:6Ainti)7 : M_i (i)8 {9 }Ten //++i Onea&operator++() A { -++m_i; - return* This; the } - //i++ - ConstAoperator++(int) - { +A tmp = * This; -++(* This);//implemented by prefix increment + returnA (TMP); A } at intm_i; - }; - int_tmain (intARGC, _tchar*argv[]) - { - inti =0; - intI1 = i++;//i1 = 0; i = 1; in intI2 = ++i;//i2 = 2; i = 1; -A A (0); toA A1 = a++;//i1 = 0; i = 1; +A A2 = ++a;//i2 = 2; i = 1; - the //a++++;//Avoid * //++++a;// Support $ASSERT (I1 = =a1.m_i);Panax NotoginsengASSERT (I2 = =a2.m_i); - return 0; the}
Description
1. The + + operation symbol overload in the class must be guaranteed to be the same as global + +.
2. In order to differentiate before and after, use + + () to indicate the former self-increment, with + + (int) after self-increment.
3. Since the "++++a" syntax should be supported as defined by the previous self-increment, and two pre-increment should be the self-operation of the A object, if a type is returned, then the second self-increment call is the pre-increment of the temporary object.
4. Post-increment should return "Const Complex". This prevents the use of shapes such as "a++++".
5. The post-increment operator function is generally implemented by the previous self-increment operation.
As can be seen from the above, the post-operation needs to copy a useless temporary object, the efficiency must be lower.