roy
roy

Reputation: 3

Assignment operator of template class c++

The assignment operator overloading is never called.

In the main file in 1., copy constructor is called and in 2., first to be call is operator + then operator * and then default assignment operator is called.

Any reason for that problem?

template<int r, int c, class F = int>
class Matrix {

public:

    Matrix(){
        int i, j;
        data = new F[(r*c)];
        for (i = 0; i < c; i++){
            for (j = 0; j < r; j++){
                data[j + (i*c)] = 0;
            }
        }
    }
    ...

    Matrix(const Matrix& a){
        int i=r, j=c;
        data = new F[(r*c)];
        for(i = 0; i < r; i++){
            for(j = 0; j < c; j++){
                data[j+(i*c)] = a.data[j+(i*c)];
            }
        }
    }
    **Matrix operator=(const Matrix a){
        int i, j;
        if (data != NULL){
            delete data;
            data = NULL;
        }
        data = new F[(r*c)];
        for(i = 0; i < r; i++){
            for(j = 0; j < c; j++){
                data[j+(i*c)] = a.data[j+(i*c)];
            }
        }
        return *this;
    }**
    friend Matrix operator+( Matrix<r, c, F>& a, int b){
        Matrix<r, c, F> temp;
        int i, j;
        for(i = 0; i < r; i++){
            for(j = 0; j < c; j++){
                temp.data[j+(i*c)] = a.data[j+(i*c)] + b;
            }
        }
        return temp;
    }
    Matrix operator*(const int a){
        Matrix <r, c, F> temp(*this);
        int i, j;
        for(i = 0; i < r; i++){
            for(j = 0; j < c; j++){
                temp.data[j+(i*c)] *= a;
            }
        }
        return temp;
    }
   ...


    ~Matrix(){
        delete[] data;
        data = NULL;
    }

private:
    F *data;


};



int main( ) {
    ...

    Matrix<4, 4> identity(1);
    std::cout << identity << std::endl;

    1.**Matrix<4, 4> res = identity;**

    Matrix<4, 4> identity_2(1);
    std::cout << identity _2<< std::endl;

    2.**Matrix<4, 4> res_2 = (identity_2 + 2) * 3;**
...

Upvotes: 0

Views: 181

Answers (1)

Guvante
Guvante

Reputation: 19221

friend Matrix operator+( Matrix<r, c, F>& a, int b){
    Matrix<r, c, F> temp;
    int i, j;
    for(i = 0; i < r; i++){
        for(j = 0; j < c; j++){
            temp.data[j+(i*c)] = a.data[j+(i*c)] + b;
        }
    }
    return temp;
}

This code is confusing the non-member and member syntax for operators.

friend Matrix operator+( Matrix<r, c, F>& a, int b);

Means "there is a non-member function that has access to my internals". But what you defined defines a member function, which should have syntax

Matrix operator+(int b) {

Since this is implied as it is a member function. See this answer for an example of discussing the nuances.

Similarly your assignment operator is incorrect.

Matrix& operator=(const Matrix& a) {

Is the most common form, although the format of the parameter can change you need to return a reference for the return value.

Upvotes: 1

Related Questions