我有屬性的類向量:刪除淺的複製對象和原始對象
class Vector
{
private:
int _capacity;
int _size;
int _resizeFactor;
int* _elements;
用這種方法:
Vector::Vector(const Vector& other)
{
this->_size = other._size;
this->_capacity = other._capacity;
this->_resizeFactor = other._resizeFactor;
delete[] this->_elements;
this->_elements = other._elements;
}
這個析構函數:
Vector::~Vector()
{
if (this->_elements)
delete[] this->_elements;
this->_elements = NULL;
}
後聲明對象,將數組插入並複製它,在程序結束時出現錯誤:
1.exe has triggered a breakpoint.
that points to the line:
delete[] this->_elements;
in the destructor.
我怎樣才能取消銷燬只有1個對象? 不改變屬性類型
你有沒有考慮使用'標準:: shared_ptr'? – Arash
您爲什麼使用'這 - >' –
如果我正確理解,不具有效果 – arik