I used to see C and Java questions such as ++ I -- ++. At that time, I understood them as follows:Prefix-type ++ I refers to the value after I is calculated and then the whole ++ I is integrated and returned, I ++ is to use the original value of I to complete the program of the eight classics, and then increase after everything is done.. The following describes the differences between prefix and suffix types through the incremental operation on custom types in C ++.
Encapsulate int class with C ++
Class newint {public: newint (): rootint (0) {}; newint (INT iniint): rootint (iniint) {}; newint & operator ++ () {cout <"prefix" <Endl; this-> rootint + = 1; return * This;} newint operator ++ (INT) // suffix, for parameter (INT) don't be confused. Int does not actually play a role, just to distinguish the prefix method, marking that the ++ operator is in the suffix FORM {cout <"Postfix" <Endl; newint Prenum = * this; // In the incremental suffix operation, you must first save the original object to an object. The prefix method is used here; ++ (* This ); // then perform incremental operations on the original object; return Prenum; // Finally, return the original metadata .} PRIVATE: int rootint ;};
Debug code:
NewInt DBInt1(12) ; ++DBInt1; cout<<DBInt1.getRootInt()<<endl;cout<<"-----**------"<<endl;DBInt1++; cout<<DBInt1.getRootInt()<<endl;
Program output:
prefix13-----**------postfixprefix14Press any key to continue
The above program basically verifies the understanding of the increment prefix and suffix at the beginning, and the source code method is clearer. When you can select both the prefix and suffix, will you select the prefix or suffix? Through the source code above, you can know a little about it. The suffix expression will generate an additional object and involve complicated copying function settings. Therefore, you can use the prefix to Increase the number.