Why an array is changed?

class MatrixClass
{
private:
 int **Matrix;
 int SizeM;
public:
MatrixClass(int);
MatrixClass(){};
~MatrixClass();

 int GetSize();
 void PrintMatrix();

 friend MatrixClass* operator+ (MatrixClass A, B MatrixClass);
 friend MatrixClass* operator- (MatrixClass A, B MatrixClass);
 // friend MatrixClass* operator* (MatrixClass A, B MatrixClass);
 // friend MatrixClass* operator/ (MatrixClass A, B MatrixClass);
}

MatrixClass::MatrixClass(int SizeM)
{
 this->SizeM = SizeM;
 Matrix = new int *[SizeM];
 for(int i = 0; i < SizeM; i++)
{
 Matrix[i] = new int [SizeM];
 for(int j = 0; j < SizeM; j++)
 Matrix[i][j] = rand() % 5;
}
}

MatrixClass::~MatrixClass()
{
 for(int i = 0; i < SizeM; i++)
{
 delete [](Matrix[i]);
}
 delete [](Matrix);
}

int MatrixClass::GetSize(){return SizeM;}

void MatrixClass::PrintMatrix()
{
 for(int i = 0; i < SizeM; i++){
 cout << "\t| ";
 for(int j = 0; j < SizeM; j++)
 cout << Matrix[i][j] << " ";
 cout << "|" << endl;
}
};

MatrixClass* operator+ (MatrixClass A, B MatrixClass)
{
 MatrixClass* C = new MatrixClass(A. GetSize());

 for(int i = 0; i < A. GetSize(); i++){
 for(int j = 0; j < A. GetSize(); j++){
 C->Matrix[i][j] = A. Matrix[i][j] + B. Matrix[i][j];
}
}

 return C;
}

MatrixClass* operator- (MatrixClass A, B MatrixClass)
{
 MatrixClass* C = new MatrixClass(A. GetSize());

 for(int i = 0; i < A. GetSize(); i++){
 for(int j = 0; j < A. GetSize(); j++){
 C->Matrix[i][j] = A. Matrix[i][j] - B. Matrix[i][j];
}
}

 return C;
}

void main()
{
clrscr();

 // 4, 10
 MatrixClass A(3), B(3), *C;

 cout<<"A: "<<endl; A. PrintMatrix(); getch();
 cout<<"B: "<<endl; B. PrintMatrix(); getch();

 C = A + B;
 cout<<"A + B: "<<endl;
 C->PrintMatrix(); getch();

 cout<<"A: "<<endl; A. PrintMatrix(); getch();
 cout<<"B: "<<endl; B. PrintMatrix(); getch();

 C = A - B;
 cout<<"A - B: "<<endl;
 C->PrintMatrix(); getch();

 // C = A * B;
 // cout<<"Overload C = A + B: ";
 // C. PrintMatrix(); getch();

 // C = A / B;
 // cout<<"Overload C = A - B: ";
 // C. PrintMatrix(); getch();
}


There is such code, I don't understand why slipping arrays A and B, although I don't even change. Change the first 2 lines of the array A and B. Tell me what the problem

5cf3ed3386978140903346.png
March 23rd 20 at 18:46
2 answers
March 23rd 20 at 18:48
Solution
The operators + and - take the objects by value and not by reference. Probably that's the problem.
Hmm, could you please explain, why this?
In fact, as I understand it, the purpose of this program that dynamically creates a new object of the matrix dimensions A. GetSize() (this is assuming that the size of the object A and B are the same), and at the end is supposed to return a REFERENCE to this object, which is assigned to the pointer P.
I am also a beginner, it is very interesting to understand this) - theresa_Kula commented on March 23rd 20 at 18:51
@Vito.Rolfson, You need a copy constructor

and at the same time change
class MatrixClass
{
private:
 int **Matrix; // <----
 int SizeM; // <----
// ...

on

private:
 int SizeM; // <----
 int **Matrix; // <----
- madalyn commented on March 23rd 20 at 18:54
There is one theory that if you pass the matrix as argument of the addition operator is copied, but the pointer remains the same, and then the copy is deleted, the destructor is called, it frees (delete[]) the memory allocated for the elements. Then need a copy to send - Vito.Rolfson commented on March 23rd 20 at 18:57
@Vito.Rolfson,
no theories or the link writes @annabel.Kessler, or copy.
int GetSize() const;
// ...
MatrixClass::MatrixClass(const MatrixClass& other)
{
 SizeM = other.GetSize();
 Matrix = new int *[SizeM];
 for(int i = 0; i < SizeM; i++)
{
 Matrix[i] = new int [SizeM];
 for(int j = 0; j < SizeM; j++)
 Matrix[i][j] = other.Matrix[i][j];
}
}
- madalyn commented on March 23rd 20 at 19:00
March 23rd 20 at 18:50
Solution
It
friend MatrixClass* operator+ (MatrixClass A, B MatrixClass);
//...
MatrixClass* operator+ (MatrixClass A, B MatrixClass)
{
//...
}


Became
friend MatrixClass* operator+ (MatrixClass& A, MatrixClass& B);
//...
MatrixClass* operator+ (MatrixClass& A, MatrixClass& B)
{
//...
}


Profit
By const-reference is necessary. You do not need to change the object you handed over. - annabel.Kessler commented on March 23rd 20 at 18:53
Profit
"Not over yet"
A = A - B;
MatrixClass D = B - A;
MatrixClass E(B + D);
- madalyn commented on March 23rd 20 at 18:56

Find more questions by tags C++