fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct A
  5. {
  6. int x;
  7. public:
  8. A() : x(0) {cout<<"Конструктор 1 this = "<<this<<endl;}
  9. A(int a) : x(a) {cout<<"Конструктор 2 this = "<<this<<endl;}
  10. ~A() {cout<<"Деструктор this = "<<this<<endl;}
  11. A(const A &rhs)
  12. {
  13. cout<<"Конструктор копирования this = "<<this;
  14. cout<<" &rhs = "<<&rhs<<endl;
  15. x=rhs.x;
  16. }
  17. A & operator = (const A &rhs)
  18. {
  19. cout<<"operator = this = "<<this;
  20. cout<<" &rhs = "<<&rhs<<endl;
  21. if(this != &rhs) x=rhs.x;
  22. return *this;
  23. }
  24. /*A operator + (const A &rhs)
  25.   {
  26.   cout<<"operator + this = "<<this;
  27.   cout<<" &rhs = "<<&rhs<<endl;
  28.   return A(x+rhs.x);
  29.   }
  30.  
  31.   A operator + (int rhs)
  32.   {
  33.   cout<<"???? operator + this = "<<endl;
  34.   return A(x+rhs);
  35.   }*/
  36.  
  37. operator int() const
  38. {
  39. cout<<"operator int() "<<this<<endl;
  40. return x;
  41. }
  42. bool operator == (const A &rhs)
  43. {
  44. cout<<"operator == this = "<<this;
  45. cout<<" &rhs = "<<&rhs<<endl;
  46. if (x==rhs.x) return true;
  47. return false;
  48. }
  49.  
  50.  
  51. int func_1() const {return x;}
  52. };
  53.  
  54.  
  55.  
  56. A operator+ (int k, const A &r)
  57. {
  58. cout<<"!!!!! operator + &r = "<<&r<<endl;
  59. return A(k+r.x);
  60. }
  61.  
  62.  
  63.  
  64.  
  65. int main()
  66. {
  67. A a;
  68. cout<<"x = "<<a.func_1()<<endl;
  69. A b;
  70. cout<<"x = "<<b.func_1()<<endl;
  71. A c;
  72. c=2+a;
  73.  
  74.  
  75.  
  76. return 0;
  77. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
Конструктор 1 this = 0xbf9d8230
x = 0
Конструктор 1 this = 0xbf9d8234
x = 0
Конструктор 1 this = 0xbf9d8238
!!!!! operator + &r = 0xbf9d8230
Конструктор 2 this = 0xbf9d823c
operator = this = 0xbf9d8238 &rhs = 0xbf9d823c
Деструктор this = 0xbf9d823c
Деструктор this = 0xbf9d8238
Деструктор this = 0xbf9d8234
Деструктор this = 0xbf9d8230