Listing 1: A very simple Matrix class
template<typename T, size_t n, size_t m>
class Matrix
{
public:
Matrix(){}
Matrix(const Matrix& rhs) {
for(int i=0; i<n; ++i)
for(int j=0; j<m; ++j)
ElementAt(i,j) = rhs.ElementAt(i,j);
}
Matrix& operator=(const Matrix& rhs) {
if( this != &rhs )
for(int i=0; i<n; ++i)
for(int j=0; j<m; ++j)
ElementAt(i,j) = rhs.ElementAt(i,j);
return *this;
}
virtual ~Matrix() {}
const T& ElementAt(size_t n, size_t m) const
{ return arrData[n][m]; }
T& ElementAt(size_t n, size_t m)
{ return arrData[n][m]; }
private:
// C-style array for efficiency and locality of reference
T arrData[n][m];
};
template<typename T, size_t n, size_t m>
Matrix<T, n, m> operator+(
const Matrix<T, n, m>& lhs,
const Matrix<T, n, m>& rhs
) {
Matrix<T, n, m> matSum;
for(int i=0; i<n; ++i)
for(int j=0; j<m; ++j)
matSum.ElementAt(i,j) =
lhs.ElementAt(i,j) + rhs.ElementAt(i,j);
return matSum;
}