fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A
  5. {
  6. friend A& operator++ (A& a);
  7. friend A operator++ (A& a, int);
  8. public:
  9. operator int() { return a_; }
  10. A& operator = (int a) { a_ = a; return *this; }
  11. private:
  12. int a_;
  13. };
  14.  
  15. A& operator++ (A& a)
  16. {
  17. ++a.a_;
  18. return a;
  19. }
  20.  
  21. A operator++ (A& a, int)
  22. {
  23. A temp = a;
  24. ++a.a_;
  25. return temp;
  26. }
  27.  
  28. int main()
  29. {
  30. A a1;
  31. a1 = 3;
  32. cout << "initial a1=" << a1 << endl;
  33. A a2 = ++a1;
  34. cout << "pre, a1=" << a1 << ", a2=" << a2 << endl;
  35. a2 = a1++;
  36. cout << "post, a1=" << a1 << ", a2=" << a2 << endl;
  37. }
  38.  
  39.  
Success #stdin #stdout 0s 15224KB
stdin
Standard input is empty
stdout
initial a1=3
pre, a1=4, a2=4
post, a1=5, a2=4