1

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?

Mykola
  • 3,343
  • 6
  • 23
  • 39
zcbd
  • 85
  • 10
  • 2
    But what is your problem? – paweln66 Jan 09 '16 at 18:03
  • Read http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom and then start from scratch. – Christian Hackl Jan 09 '16 at 18:12
  • And/or implement your matrix with `std::vector` and let it all be handled automatically. – Christian Hackl Jan 09 '16 at 18:13
  • I was edit my question, please take a look – zcbd Jan 09 '16 at 18:16
  • 2
    What is it you are trying to assign - the whole other matrix, or a single element? The fact that the operator takes `T` (as opposed to `Matrix`) as a parameter suggests the latter - but then `this == &obj` check makes no sense (`this` and `&obj` are of different types). – Igor Tandetnik Jan 09 '16 at 18:22
  • The expression `m[i][j] = m2[i][j]` doesn't call `operator=` on `Matrix`. It calls `operator[]` on `Matrix`, then `operator[]` on whatever the first call returns; then another pair of `operator[]` calls for the other side of the assignment; and finally, `operator=` for individual element. Your `Matrix` class does provide `operator[]` implementation, right? – Igor Tandetnik Jan 09 '16 at 18:25
  • for every template matrix I have an array.. this is the code: "this->_array[i][j] = other._array[i][j];" – zcbd Jan 09 '16 at 18:30

1 Answers1

0

The problem of your code is that you trying to assign the Matrix<T> by reference of T, so Matrix<T> object will didnot be affected by assignments.

In design you must always return Matrix<T>& from your assignment operator. So the code must look like this.

template<class T>
inline Matrix<T>& Matrix<T>::operator=(const T &obj)
{
    // Some object member assignment
    m_SomeMember = obj;

    return *this;
}

You in this case must not check objects for dublicates because T is not Matrix<T>, its just reassign object with different type member.

If you still want to assign Matrix<T> object with another Matrix<T>. You code must look like:

template<class T>
inline Matrix<T>& Matrix<T>::operator=(const Matrix<T>& obj)
{
    if (this == &obj)
    {
        return *this;
    }
    // Performing deep copying
    m_SomeMember = obj.m_SomeMember;
    return *this;
}

I have also detected problems with your copy constructor. You must use:

template<class T>
inline Matrix<T>::Matrix(const Matrix<T>& other)
{
    /* Copy constructors body */
}

instead of

template<class T>
inline Matrix<T>::Matrix(const Matrix& other)
{
    /* Copy constructors body */
}
Mykola
  • 3,343
  • 6
  • 23
  • 39