1. // ++运算符重载
    2. class A{
    3. public:
    4. //友元函数声明
    5. friend ostream& operator<<(ostream& os,const A& a);
    6. // 构造函数
    7. A(int a ){
    8. this->data =a ;
    9. }
    10. //拷贝构造函数
    11. A(const A& a){
    12. this->data = a.data;
    13. cout << "in copy constructor " << endl;
    14. }
    15. ~A(){
    16. cout<< "~A" << endl;
    17. }
    18. // 前置 返回的是自己的引用
    19. A& operator++(){
    20. this->data++;
    21. return *this;
    22. }
    23. //后置 返回的是临时变量 int是个 哑元,用来区分前置和后置
    24. const A operator++(int){
    25. A temp(*this);
    26. ++(*this);
    27. return temp;
    28. }
    29. private:
    30. int data;
    31. };
    32. ostream& operator<<(ostream& os,const A& a){
    33. os << a.data <<endl;
    34. return os;
    35. }
    36. int main(){
    37. A a(1);
    38. cout << a <<endl;
    39. cout << "----------" <<endl;
    40. A b= a++;
    41. cout << b <<endl;
    42. cout << "----------" <<endl;
    43. A c =++b;
    44. cout<<c<<endl;
    45. return 0;
    46. }