title: C++ Matrix Class - Suggestions
body: <p>I am trying to build a templated Matrix class in C++. Below is its implementation.
  I implemented two operators +,+= as of now just to give an idea on how it will look and I thought it would be best to ask for feedback before I proceed any further.</p>
  
  <p>The whole implementation is public and also there are no explicit bound/error checks, this is because its not meant to be a full fledged matrix library and hence avoided unnecessary code.</p>
  
  <p>It would be very helpful if someone could comment on it and may be suggest a few improvements or suggestions.</p>
  
  <p>Thank you.</p>
  
  <pre><code>template&lt;class T&gt;
  class Matrix
  {
      public:
      int r,c;
      vector&lt; vector&lt; T &gt; &gt;mat;
  
      Matrix() {}
  
      // Constructor to set the size of the matrix
      Matrix(int _r,int _c)
      {
          r=_r;c=_c;
          mat.resize(r);
          for(int i=0;i&lt;r;i++)
              mat[i].resize(c);
      }
      // Constructor to build a matrix from a C 2d array
      // Pointer to the first element is passed (&amp;arr[0][0])
      Matrix(T *arr,int _r,int _c)
      {
          r=_r;c=_c;
          mat.resize(r);
          for(int i=0;i&lt;r;i++)
              for(int j=0;j&lt;c;j++)
                  mat[i].push_back(arr[i*c+j]);
      }
      template&lt;typename U&gt;
      Matrix&lt;T&gt;&amp; operator +=(const Matrix&lt;U&gt;&amp;M)
      {
          for(int i=0;i&lt;r;i++)
              for(int j=0;j&lt;c;j++)
                  mat[i][j]+=static_cast&lt;T&gt;(M.mat[i][j]);
          return *this;
      }
      template&lt;typename U&gt;
      Matrix&lt;T&gt; operator +(const Matrix&lt;U&gt;&amp;M)
      {
          Matrix&lt;T&gt;tmp=*this;
          return tmp+=M;
      }
  };
  
  template&lt;typename T&gt;
  istream&amp; operator &gt;&gt;(istream &amp;in,Matrix&lt;T&gt;&amp;M)
  {
      in&gt;&gt;M.r&gt;&gt;M.c;
      Matrix&lt;T&gt;tmp(M.r,M.c);
      for(int i=0;i&lt;M.r;i++)
          for(int j=0;j&lt;M.c;j++)
              in&gt;&gt;tmp.mat[i][j];
      M=tmp;
      return in;
  }
  template&lt;typename T&gt;
  ostream&amp; operator &lt;&lt;(ostream &amp;out,Matrix&lt;T&gt;M)
  {
      for(int i=0;i&lt;M.r;i++)
      {
          for(int j=0;j&lt;M.c;j++)
              cout&lt;&lt;M.mat[i][j]&lt;&lt;" ";
          cout&lt;&lt;endl;
      }
      return out;
  }
  </code></pre>
  
  <p>EDIT:
  Thank you all for the suggestions. </p>
  
  <p>I just have one small question, say I do want to implement error checking (ex: checking for bounds,valid arguments etc..) however I do want the provide user with an option to disable error checking completely, is there any good way to implement this ?
  What I need is something like example:`ios_base::sync_with_stdio(0);.
  Thanks again.</p>