vendredi 13 novembre 2020

can't delete dynamic array from class

Vector& Vector::operator=(const Vector& other)
{
    int i = 0;

    delete[] this->_elements; // this line is bad !

    // copying all the fields:
    this->_size = other._size;
    this->_capacity = other._capacity;
    this->_resizeFactor = other._resizeFactor;

    this->_elements = new int[other._capacity]; // allocating new memory for the array.

    for (i = 0; i < this->_capacity; i++) // copying the array's.
    {
        this->_elements[i] = other._elements[i];
    }

    return *this;
}

I am trying to implement the '=' operator to deep copy objects, when I am trying to delete the old array I get this problem: enter image description here

I can delete this line and all will works fine but then I will have a memory leaks.

Aucun commentaire:

Enregistrer un commentaire