【文章內(nèi)容簡(jiǎn)介】
其他用途,只是用于區(qū)分前置運(yùn)算與后置運(yùn)算。 ? 例: 成員函數(shù)重載示例 。 include class Increase{ public: Increase(int x):value(x){} Increase amp。 operator++()。 //前增量 Increase operator++(int)。 //后增量 void display(){ cout the value is value endl。 } private: int value。 }。 Increase amp。 Increase::operator++() { value++。 //先增量 return *this。 //再返回原對(duì)象 } Increase Increase::operator++(int) { Increase temp(*this)。 //臨時(shí)對(duì)象存放原有對(duì)象值 value++。 //原有對(duì)象增量修改 return temp。 //返回原有對(duì)象值 } void main() { Increase n(20)。 ()。 (n++).display()。 //顯示臨時(shí)對(duì)象值 ()。 //顯示原有對(duì)象 ++n。 ()。 ++(++n)。 ()。 (n++)++。 //第二次增量操作對(duì)臨時(shí)對(duì)象進(jìn)行 ()。 } 此程序的運(yùn)行結(jié)果為: the value is 20 the value is 20 the value is 21 the value is 22 the value is 24 the value is 25 ? 例: 友元函數(shù)重載示例 。 include class Increase{ public: Increase(int x):value(x){} friend Increase amp。 operator++(Increase amp。 )。 //前增量 friend Increase operator++(Increase amp。,int)。 //后增量 void display(){ cout the value is value endl。 } private: int value。 }。 Increase amp。 operator++(Increase amp。 a) { ++。 //前增量 return a。 //再返回原對(duì)象 } Increase operator++(Increaseamp。 a, int) { Increase temp(a)。 //通過(guò)拷貝構(gòu)造函數(shù)保存原有對(duì)象值 ++。 //原有對(duì)象增量修改 return temp。 //返回原有對(duì)象值 } void main() { Increase n(20)。 ()。