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?