1 #include<iostream> 2 using namespace std; 3 4 class Complex{ 5 public: 6 Complex(int a,int b){ 7 this->a=a; 8 this->b=b; 9 } 10 11 void printComplex(){ 12 cout<<"("<<this->a<<","<<this->b<<"i)"<<endl; 13 } 14 friend Complex& operator++(Complex& c);/*友元函数声明*/ 15 friend const Complex operator++(Complex& c,int); 16 /* 17 局部中的重载前++运算符 18 Complex& operator++(){ 19 this->a++; 20 this->b++; 21 22 return *this; 23 } 24 */ 25 /* 26 局部中的重载后++运算符 27 const Complex operator++(int){//亚元(即占位符,坑啊~~~) 28 Complex temp(this->a,this->b); 29 this->a++; 30 this->b++; 31 32 return temp; 33 } 34 */ 35 private: 36 int a; 37 int b; 38 }; 39 40 /*重载前++运算符*/ 41 Complex& operator++(Complex& c){ 42 c.a++; 43 c.b++; 44 return c; 45 } 46 /*重载后++运算符,后++不可以多次进行*/ 47 const Complex operator++(Complex& c,int){//int 为占位符 48 Complex temp(c.a,c.b); 49 c.a++; 50 c.b++; 51 52 return temp; 53 } 54 55 int main(){ 56 Complex c1(1,2); 57 ++++c1;//可以多次进行前置++ 58 c1.printComplex(); 59 c1++; 60 c1.printComplex(); 61 62 return 0; 63 }
关于c++中前++后++运算符重载问题
猜你喜欢
转载自www.cnblogs.com/Bravewtz/p/10335774.html
今日推荐
周排行