All pastes #2073805 Raw Edit

Something

public text v1 · immutable
#2073805 ·published 2011-06-02 18:39 UTC
rendered paste body
#ifndef MATRIX_H
#define MATRIX_H

template<int size,typename T>
class Matrix
{

public:
    typedef T value_type;

    explicit Matrix(const T &val = T());
    Matrix(const Matrix &);
    template<typename C>
    Matrix(const Matrix<size,C> &another)
    {
        this->assign(another);
    }

    void assign(const Matrix &);
    template<typename C>
    void assign(const Matrix<size,C> &another)
    {
        for(int i=0;i<size;++i)
            for(int j=0;j<size;++j)
                m[i][j] = another.m[i][j];
    }

    void fill(const T &val);

    T &operator()(int row,int column){return m[row][column];}
    const T &operator()(int row,int column) const {return m[row][column];}

    Matrix &operator=(const Matrix &another){ this->assign(another); return *this;}
    template<typename C>
    Matrix &operator=(const Matrix<size,C> &another){ this->assign(another); return *this;}

private:

    T m[size][size];
};

template<int size,typename T>
Matrix<size,T>::Matrix(const T &val)
{
    this->fill(val);
}
template<int size,typename T>
Matrix<size,T>::Matrix(const Matrix & another)
{
    this->assign(another);
}

template<int size,typename T>
void Matrix<size,T>::fill(const T &val)
{
    for(int i=0;i<size;++i)
        for(int j=0;j<size;++j)
            m[i][j] = val;
}

template<int size,typename T>
Matrix<size,T> &Matrix<size,T>::assign(const Matrix &another)
{
    for(int i=0;i<size;++i)
        for(int j=0;j<size;++j)
            m[i][j] = another.m[i][j];
}

#endif // MATRIX_H