fork download
  1. #include <iostream>
  2.  
  3. class X {
  4. public:
  5. int x;
  6.  
  7. X(int x) :
  8. x(x) {
  9.  
  10. std::cout << "X::X(int), x = " << x << '\n';
  11. }
  12.  
  13. X(const X& other) :
  14. x(other.x) {
  15.  
  16. std::cout << "X::X(const X &), x = " << x << ", other.x = " << other.x << '\n';
  17. }
  18.  
  19. X &operator=(const X& other) {
  20. std::cout << "X::operator=(const X &), x = " << x << ", other.x = " << other.x << '\n';
  21. if (&other == this) {
  22. return *this;
  23. }
  24.  
  25. x = other.x;
  26.  
  27. return *this;
  28. }
  29.  
  30. ~X() {
  31. std::cout << "X::~X(int), x = " << x << '\n';
  32. }
  33. };
  34.  
  35. X fun(X obiekt) {
  36. // Obiekt tymczasowy:
  37. return X(obiekt.x * 2);
  38. }
  39.  
  40. int main() {
  41.  
  42. std::cout << "obiekt\n";
  43. X obiekt(5);
  44.  
  45. std::cout << "przypisanie\n";
  46. obiekt = 10;
  47.  
  48. std::cout << "wywolanie funkcji\n";
  49. fun(obiekt);
  50.  
  51. std::cout << "wywolanie funkcji z obiektem tymczasowym\n";
  52. obiekt = fun(X(30));
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
obiekt
X::X(int), x = 5
przypisanie
X::X(int), x = 10
X::operator=(const X &), x = 5, other.x = 10
X::~X(int), x = 10
wywolanie funkcji
X::X(const X &), x = 10, other.x = 10
X::X(int), x = 20
X::~X(int), x = 20
X::~X(int), x = 10
wywolanie funkcji z obiektem tymczasowym
X::X(int), x = 30
X::X(int), x = 60
X::operator=(const X &), x = 10, other.x = 60
X::~X(int), x = 60
X::~X(int), x = 30
X::~X(int), x = 60