How to overload the Assignment operator so that the Original Objects's values are unchanged? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to overload the Assignment operator so that the Original Objects's values are unchanged?

I have a Matrix Class with a following function: template<class M> class Matrix { private: int r,c; M **mat; public: Matrix(int row=1,int col=1):r(row),c(col) { mat = new M*[r]; for (int i = 0; i < r; ++i) { mat[i] = new M[c]; for (int j = 0; j < c; ++j) mat[i][j] = 0; } } Matrix(initializer_list<initializer_list<M>> lst) : Matrix(lst.size(), lst.size() ? lst.begin()->size() : 0) { int i = 0, j = 0; for (const auto& l : lst) { for (const auto& v : l) { mat[i][j]=v; ++j; } j = 0; ++i; } } Matrix operator=(const Matrix Te) { r=Te.r; c=Te.c; mat=Te.mat; return *this; } ///Many Other Useful Functions... }; Now, I try to perform the following operations: Matrix<ld>a(3,3),b(3,3); cin>>a; b=a; b(3,3)=24.6; cout<<a<<b; But, the operation b(3,3)=24.6 changes the element at 3,3 in a as well to 24.6, which I didn't want to happen. So how can I repair the = overload so that the oriiginal values remain the same? Why is this definition changing a as well?

14th May 2017, 9:51 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
17 Answers
+ 7
I am going to have so many notes from aloisia and you Kinshuk
14th May 2017, 12:07 PM
jay
jay - avatar
+ 5
:) you can do it! wish i could help. but you are well ahead of me in learning
14th May 2017, 11:05 AM
jay
jay - avatar
+ 5
On correcting that and visualizing, I saw that both matrices now use the same pointer mat... I don't understand how both got the same links...
14th May 2017, 11:44 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
It is done! Thank You ‎ɐısıօլɐ! I retried using a for loop instead of mat=Te.mat, and it worked perfectly! Here is the code: Matrix& operator=(const Matrix& Te) { if (this == &Te) return *this; r=Te.r; c=Te.c; for(int i=0;i<r;i++) for(int j=0;j<c;j++) mat[i][j]=Te.mat[i][j]; return *this; }
14th May 2017, 11:51 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
You're great...
14th May 2017, 12:35 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
Check out my beta version : https://code.sololearn.com/cq24J951TxgI/?ref=app It would've never been possible without your help...
14th May 2017, 12:36 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
I get an error on marking a as const. That - cannot bind ostream lvalue to rvalue reference...
14th May 2017, 11:32 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
That is strange ... if you try to declare a as const or constexpr, would that change something? Maybe your assigning the pointer and not copying it, so you're basically operating on the same matrix
14th May 2017, 10:16 AM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 3
Matrix& operator=(const Matrix& Te) /// Assigns M2 with the values of M1 { if(this=&Te) return *this; r=Te.r; c=Te.c; mat=Te.mat; return *this; } Even this Fails... I'm following these steps currently: 1)Take a const-reference for the argument (the right-hand side of the assignment). 2)Return a reference to the left-hand side, to support safe and reasonable operator chaining. (Do this by returning *this.) 3)Check for self-assignment, by comparing the pointers (this to &rhs).
14th May 2017, 10:51 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
I tried assigning values using a simple for loop, but that failed as well...
14th May 2017, 10:57 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
@jay Thank You!
14th May 2017, 11:07 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
https://www.sololearn.com/Discuss/388679/?ref=app if you have one file it might work, if you put the main file in there
14th May 2017, 11:10 AM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 3
I get this on uploading my entire code (>2000 lines) Server error! Your code might be too long for this tool. Shorten your code and re-try. Ill retry with just the useful part...
14th May 2017, 11:13 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
I knew it (with the pointer) glad it works now, always happy to help 🤗
14th May 2017, 12:35 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 3
I'm not that great, you figured it mostly out on your own :)
14th May 2017, 12:38 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 2
maybe try to put your code into the visualizer I posted earlier to see what your code does?
14th May 2017, 11:07 AM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 2
visualizer? Where? I am unable to find that... Please upload it again...
14th May 2017, 11:08 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar