title: Const operator overloading problems in C++
body: <p>I'm having trouble with overloading operator() with a const version:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  class Matrix {
  public:
      Matrix(int m, int n) { 
          vector&lt;double&gt; tmp(m, 0.0);
          data.resize(n, tmp);
      }
      ~Matrix() { }
  
  
      const double &amp; operator()(int ii, int jj) const {
          cout &lt;&lt; " - const-version was called - ";
          return data[ii][jj];
      }
  
      double &amp; operator()(int ii, int jj) {
          cout &lt;&lt; " - NONconst-version was called - ";
          if (ii!=1) {
              throw "Error: you may only alter the first row of the matrix.";
          }
          return data[ii][jj];
       }
  
  
  protected:  
      vector&lt; vector&lt;double&gt; &gt; data;
  };
  
  int main() {
  try {
      Matrix A(10,10);
      A(1,1) = 8.8;
      cout &lt;&lt; "A(1,1)=" &lt;&lt; A(1,1) &lt;&lt; endl;
      cout &lt;&lt; "A(2,2)=" &lt;&lt; A(2,2) &lt;&lt; endl;
      double tmp = A(3,3);
  } catch (const char* c) { cout &lt;&lt; c &lt;&lt; endl; }
  }
  </code></pre>
  
  <p>This gives me the following output:</p>
  
  <blockquote>
    <ul>
    <li>NONconst-version was called -  - NONconst-version was called - A(1,1)=8.8</li>
    <li>NONconst-version was called - Error: you may only alter the first row of the matrix.</li>
    </ul>
  </blockquote>
  
  <p>How can I achieve that C++ call the const-version of operator()? I am using GCC 4.4.0.</p>