一、全局函数重载++(前置) Complex & operator++(Complex &c1) { c1.a++;//由于用到了类中的私有成员,该函数需要成为类的友元函数 c1.b++; return c1; }
二、成员函数重载++(前置) Complex & operator++() { this->a++; this->b++; return *this; }
三、全局函数重载++(后置) Complex operator++(Complex &c1,int)//为了和前置的函数区分开,添加了占位符int { Complex tmp=c1;//后置++:先使用,后++ c1.a++;//友元函数 c1.b++; return tmp; }
四、成员函数重载++(后置) Complex operator++(int) { Complex tmp=*this; this->a++; this->b–; return tmp; }