fork download
  1. #include <utility>
  2. #include <cstddef>
  3. #include <algorithm>
  4.  
  5. class ScopedIntArray // этот класс нельзя копировать, то можно перемещать
  6. {
  7. public:
  8. explicit ScopedIntArray(size_t n)
  9. : raw_(new int[n]()), size_(n)
  10. { }
  11. ScopedIntArray()
  12. : raw_(nullptr), size_(0)
  13. { }
  14.  
  15. ~ScopedIntArray()
  16. {
  17. delete[] raw_;
  18. }
  19. int & operator[](size_t idx)
  20. {
  21. return raw_[idx];
  22. }
  23. int operator[](size_t idx) const
  24. {
  25. return raw_[idx];
  26. }
  27. size_t size() const
  28. {
  29. return size_;
  30. }
  31. int * begin() const
  32. {
  33. return raw_;
  34. }
  35. int * end() const
  36. {
  37. return raw_ + size_;
  38. }
  39. // Из-за того, что мы не определяли свои
  40. // собственные оператор присваивания\конструктор компирования
  41. // генерируемые по-умолчанию были автоматически помечены как deleted,
  42. // поэтому в этом случае яное указание noncopyable не требуется.
  43.  
  44. ScopedIntArray(ScopedIntArray && other)
  45. : raw_(other.raw_), size_(other.size_)
  46. {
  47. other.raw_ = 0;
  48. other.size_ = 0;
  49. }
  50. ScopedIntArray & operator=(ScopedIntArray && other)
  51. {
  52. if(this != &other)
  53. {
  54. std::swap(raw_, other.raw_);
  55. std::swap(size_, other.size_);
  56. }
  57. return *this;
  58. }
  59.  
  60. private:
  61. int * raw_;
  62. size_t size_;
  63. };
  64.  
  65. class ScopedIntArrayExt // этот класс можно копировать и перемещать
  66. : public ScopedIntArray
  67. {
  68. public:
  69. ScopedIntArrayExt()
  70. : ScopedIntArray(), additionalData_(false)
  71. { }
  72. explicit ScopedIntArrayExt(size_t n, bool additionalData = false)
  73. : ScopedIntArray(n), additionalData_(additionalData)
  74. { }
  75. ScopedIntArrayExt(ScopedIntArrayExt const & other)
  76. : ScopedIntArray(other.size()), additionalData_(other.additionalData_)
  77. {
  78. std::copy(other.begin(), other.end(), begin());
  79. }
  80. ScopedIntArrayExt & operator=(ScopedIntArrayExt const & other)
  81. {
  82. if(this != &other)
  83. {
  84. ScopedIntArray::operator=(ScopedIntArrayExt(other)); // copy-and-swap idiom
  85. additionalData_ = other.additionalData_;
  86. }
  87. return *this;
  88. }
  89.  
  90. private:
  91. bool additionalData_;
  92. };
  93.  
  94. int main()
  95. {
  96. ScopedIntArray arr(30);
  97. arr[10] = 2;
  98.  
  99. //ScopedIntArray arr1 = arr; // ERROR use of deleted function 'ScopedIntArray::ScopedIntArray(const ScopedIntArray&)'
  100. ScopedIntArray arr1 = std::move(arr); // ОК, перемещение
  101.  
  102. ScopedIntArray arr2;
  103.  
  104. //arr = arr2; // ERROR: use of deleted function 'ScopedIntArray& ScopedIntArray::operator=(const ScopedIntArray&)'
  105. arr = std::move(arr2); // ОК, перемещение
  106.  
  107. ScopedIntArrayExt arr3(30);
  108. arr3[11] = 3;
  109.  
  110. ScopedIntArrayExt arr4 = arr3; // OK, копирование
  111. ScopedIntArrayExt arr5 = std::move(arr3); // OK, перемещение
  112. ScopedIntArrayExt arr6;
  113.  
  114. arr3 = arr6; // ОК, присваивание
  115. arr3 = std::move(arr6); // ОК, перемещение
  116. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty