I'm trying to write an overload to operator =
for templates objects,
I'm creating a template matrix. I need that if I will do something like: m[i][j] = m2[i][j];
it should work with any kind of parameters and objects.
this is my code:
copy constructor:
template<class T>
inline Matrix<T>::Matrix(const Matrix& other)
{
this->_rows = other._rows;
this->_cols = other._cols;
this->_array = new T*[rows];
for (int i = 0; i < rows; i++)
this->_array[i] = new T[cols];
// Copy 'temp' to 'this'
for (int i = 0; i < this->_rows; i++)
for (int j = 0; j < this->_cols; j++)
this->_array[i][j] = other._array[i][j];
}
operator=
overload:
template<class T>
inline T& Matrix<T>::operator=(const T &obj)
{
// Is the same
if (this == &obj)
{
return *this;
}
// Go to copy constructor of 'T'
T* temp = new T(obj);
return temp;
}
can you tell me what I need to change or fix please?