标签:
代码:
1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 class one{ 7 public: 8 one(int i); 9 void print(); 10 friend one operator++(one&); 11 friend one operator++(one&,int); 12 13 private: 14 int i; 15 }; 16 17 one::one(int I){ 18 i = I; 19 } 20 21 void one::print(){ 22 cout<<"i="<<i<<endl; 23 } 24 25 one operator++(one &op){ 26 ++op.i; 27 return op; 28 } 29 30 one operator++(one &op,int){ 31 one temp(op); 32 op.i++; 33 return temp; 34 } 35 36 int main(){ 37 one obj1(1),obj2(100); 38 obj1.print(); 39 (++obj1).print();//隐式调用 40 (obj1++).print(); 41 obj1.print(); 42 43 (operator++(obj2)).print();//显式调用 44 (operator++(obj2,1)).print();//此处的参数1可以取任意整数(int) 45 obj2.print(); 46 47 return 0; 48 }
输出:
i=1 i=2 i=2 i=3 i=101 i=101 i=102
分析:
前缀方式和后缀方式重载函数不同,以参数int区分
标签:
原文地址:http://www.cnblogs.com/hu983/p/5410750.html