fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct HasAResource
  5. {
  6. int* arr;
  7.  
  8. HasAResource(int size):
  9. arr(new int [size])
  10. {
  11. cout << "size constructor: " << this << endl;
  12. }
  13.  
  14. HasAResource(const HasAResource& other) = delete;
  15. HasAResource& operator=(const HasAResource& other) = delete;
  16.  
  17. HasAResource(HasAResource&& other):
  18. arr(nullptr)
  19. {
  20. arr = other.arr;
  21. other.arr = nullptr;
  22. cout << "move constructor: " << this << endl;
  23. }
  24.  
  25. HasAResource& operator=(HasAResource&& other)
  26. {
  27. cout << "move assignment: from " << &other << " to " << this << endl;
  28. if (this != &other)
  29. {
  30. delete[] arr;
  31. arr = other.arr;
  32. other.arr = nullptr;
  33. }
  34. return *this;
  35. }
  36.  
  37. ~HasAResource()
  38. {
  39. cout << "destructor: " << this << endl;
  40. delete[] arr;
  41. }
  42.  
  43. HasAResource& doStuff()
  44. {
  45. cout << "doStuff: " << this << endl;
  46. return *this;
  47. }
  48. };
  49.  
  50. int main()
  51. {
  52. HasAResource x = std::move(HasAResource(42).doStuff());
  53. return 0;
  54. }
Success #stdin #stdout 0.01s 5504KB
stdin
Standard input is empty
stdout
size constructor: 0x7ffe89f98fc0
doStuff: 0x7ffe89f98fc0
move constructor: 0x7ffe89f98fb8
destructor: 0x7ffe89f98fc0
destructor: 0x7ffe89f98fb8