简体   繁体   中英

operator = overload in template class

Im working on matrix template class, and now i should write the "=" operator overload. What Im trying to do is to delete the matrix that appears in the left side of the '=', and return new one that equals to the matrix that appears in the right side of the '='.

because i can't delete "this" with a distructor, I delete it "manually" in the function. but now i should make a new matrix, therefor i make a new one ("temp") and return it. The prblem is that "temp" is really return, but it doesn't set in the matrix that appears in the left side of the '='.

The code:

Matrix<int> m (3, 4);
    Matrix<int> m2(2, 5);
    m2 = m;

This was the main part.

The function:

template<class T>
Matrix<T> & Matrix<T>::operator=(Matrix<T>& mat)
{
    if (this==&mat)
    {
        return *this;
    }

    for (int i = 0; i < this->rows; i++)
    {
        delete[] this->mat[i];
    }

    delete[] this->mat;

    Matrix<T> * temp = new Matrix<T>(mat.rows, mat.cols);

    for (int i = 0; i < temp->rows; i++)
        for (int j = 0; j < temp->cols; j++)
        {
            temp->mat[i][j] = mat.mat[i][j];
        }

    return *temp;
}


template<class T>
Matrix<T>::Matrix(int row, int col)
{
    rows = row;
    cols = col;
    mat = new T*[rows];
    for (int i = 0; i < rows; i++)
    {
        mat[i] = new T[cols];
    }
    rester(*this);
}

Thx!!

Use std::vector as storage (instead of manal new and delete ), and just accept the copy assignment operator generated by the compiler. It's that easy.


If you absolutely want to implement the copy assignment yourself, for learning , then just express copy assignment in terms of copy construction.

To do that, first define a noexcept swap operation:

// In class definition:
friend
void swap( Matrix& a, Matrix& b )
    noexcept
{
    using std::swap;
    // swap all data members here
}

Then the copy assignment operator can be expressed as simply

// In class definition
auto operator=( Matrix other )
    -> Matrix&
{
    swap( *this, other );
    return *this;
}

It's popular, an idiom, because it's very simple yet exception safe.


Instead of returning a reference, which adds verbosity and some possible marginal inefficiency for no gain, you might want to just use void as return type. However, probably for historical reasons, the containers in the standard library require the copy assignment operator to return a reference to self.

You need to allocate memory for this instead of creating a temp .

template<class T>
Matrix<T> & Matrix<T>::operator=(Matrix<T>& rhs)
{
    if (this==&rhs)
    {
        return *this;
    }

    // Delete current memory
    for (int i = 0; i < this->rows; i++)
    {
        delete[] this->mat[i];
    }

    delete[] this->mat;

    this->rows = rhs.rows;
    this->cols = rhs.cols;

    // Allocate new memory
    // Assign values to newly allocated memory.
    this->mat = new int*[rhs.rows];
    for (int = 0; i < rhs.rows; ++i )
    {
       this->mat[i] = new int[rhs.cols];
       for (int j = 0; j < rhs.cols; j++)
       {
           this->mat[i][j] = rhs.mat[i][j];
       }
    }

    // Return *this.
    return *this;
}

I would recommend using the suggestion given in the answer by @Cheersandhth .

Also use a different name of the argument. Don't confuse with the member variable mat and the argument mat .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM