#include <iostream> #include <cmath> #include <ctime> class Matrix { int **ptr = nullptr; int x = NULL, y = NULL; public: Matrix() = default; Matrix(int i, int j) :x(i), y(j) { ptr = new int*[x]; for (int i = 0; i < x; i++) ptr[i] = new int[y]; } Matrix(int i) :Matrix(i, i) {} Matrix(const Matrix &ob) : x(ob.x), y(ob.y) { //выделяем память для копии. ptr = new int*[ob.x]; for (int i = 0; i < ob.x; i++) ptr[i] = new int[ob.y]; //копируем. for (int i = 0; i < ob.x; i++) for (int j = 0; j < ob.y; j++) ptr[i][j] = ob.ptr[i][j]; } Matrix operator=(Matrix rhs); ~Matrix(); void fill(); void show(); }; Matrix Matrix::operator=(Matrix rhs) { for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) ptr[i][j] = rhs.ptr[i][j]; return *this; } Matrix::~Matrix() { for (int i = 0; i < x; i++) delete[] ptr[i]; delete[] ptr; } void Matrix::fill() { std::srand(std::time(NULL)); for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) ptr[i][j] = rand() % 10; } } void Matrix::show() { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { std::cout << ptr[i][j] << ' '; } std::cout << std::endl; } std::cout << std::endl; } int main() { Matrix A(2), B; A.fill(); A.show(); B = A; std::cout << std::endl; B.show(); return 0; } 2 answers
Matrix B is created by the default constructor, i.e. it has a null pointer, and x and y are zero. Those. in the assignment operator
for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) ptr[i][j] = rhs.ptr[i][j]; for this reason, nothing is done (and well, because you have zero ptr ).
Here you get nothing :)
But you still have, if not mistakes, then absurdities ...
For example, why does the assignment operator get a copy of the matrix to be assigned? It would make sense with this implementation:
{ swap(x,rhs.x); swap(y,rhs.y); swap(ptr,rhs.ptr); } And more - why does he return a copy of your matrix?
NULL used for pointers (or rather, nullptr ), so using this value instead of zero to initialize int values is, to put it mildly, absurd.
- Well, I will think, the teacher told Me that it is better to assign the variables to NULL, and to the signs nullptr - Ilya
You must correctly define this copy assignment operator. Its definition may look like this.
Matrix & Matrix::operator =( const Matrix &rhs ) { if ( this != &rhs ) { int **tmp = new int* [rhs.x]; for ( int i = 0; i < rhs.x; i++ ) tmp[i] = new int[rhs.y]; if ( ptr != nullptr ) { for ( int i = 0; i < x; i++ ) delete [] ptr[i]; } delete [] ptr; x = rhs.x; y = rhs.y; ptr = tmp; for ( int i = 0; i < x; i++ ) { for ( int j = 0; j < y; j++ ) ptr[i][j] = rhs.ptr[i][j]; } } return *this; } It is advisable to place and create arrays addressed by a data member of the ptr class into separate functions so that the corresponding code is not duplicated, since it is used not only in one copying assignment operator.
You could also write a relocation constructor and a reassignment operator.
Note that the destructor is not written correctly, since in general ptfr may be equal to nullptr . It will be correct to write the destructor as follows
Matrix::~Matrix() { if ( ptr !+ nullptr ) { for (int i = 0; i < x; i++) delete[] ptr[i]; } delete[] ptr; } - You delete and then re-allocate memory for the object, but I allocated memory in the copy constructor. Or in this form the copy constructor is not needed? - Ilya
- @Ilya The copy constructor is used when creating a new object. A copying assignment operator is used for an object already created that already has initialized data members. - Vlad from Moscow
- @Ilya I showed you how to write an assignment beautifully - in 3 (!) Lines. Look again - when passing by value, you ALREADY copied the matrix. Then simply exchange its fields in 3 lines, and the copy destructor will take care of deleting your old object. And do not re-write the allocation of memory and other gestures, if they already exist in the copy constructor! - Harry
- @Ilya Look here - ideone.com/GjqQ95 - this is all the assignment for you, and everything works :) By the way, it will work correctly even if
&rhs == this. - Harry - @Ilya I made some changes to the definition of the operator, since in the general case the pointer ptr may be equal to nullptr, and therefore it is impossible to delete the elements ptr [i] - Vlad from Moscow
