Fix copy and move assigments of Array

This commit is contained in:
topjohnwu 2018-11-07 04:09:37 -05:00
parent 803c5377a6
commit 59ba350f34

View File

@ -67,17 +67,30 @@ public:
T* _node; T* _node;
}; };
T& operator = (const T& a) { Array &operator=(const Array& a) {
delete [] _data;
_data = nullptr;
_size = a._size; _size = a._size;
_capacity = a._capacity; _capacity = a._capacity;
if (_capacity) { if (_capacity) {
_data = new T[_capacity]; _data = new T[_capacity];
for(int i = 0; i < _size; ++i) for(int i = 0; i < _size; ++i)
_data[i] = utils::move(a[i]); _data[i] = a[i];
} }
return *this; return *this;
} }
Array &operator=(Array&& a) {
delete [] _data;
_size = a._size;
_capacity = a._capacity;
_data = a._data;
a._size = 0;
a._capacity = 0;
a._data = nullptr;
return *this;
}
iterator begin() const { return iterator(_data); } iterator begin() const { return iterator(_data); }
iterator end() const { return iterator(_data + _size); } iterator end() const { return iterator(_data + _size); }