Your assignment operator is returning *this
by value, making a copy of it. Normally, you would return a reference:
Matrix& Matrix::operator=(const Matrix& A)
// ^
The copy is made using the implicit copy constructor, which simply copies the member pointer. You now have two objects which both think they "own" the allocated memory. The temporary value returned by operator=
is soon destroyed, deleting the memory, and leaving y
with a dangling pointer; you get the error when y
tries to delete the same memory a second time.
This is why classes that manage resources should follow the Rule of Three, to ensure that they are safe to copy. Standard containers such as std::vector
do this; it's usually a good idea to use these rather than managing memory yourself.