title: overloading [][] operators in c++
body: <p>I'm writing a matrix 3x3 class in c++.</p>
  
  <p>glm::mat3 provides access to matrix data through the <code>[][] operator</code> syntax.<br>
  e.g. <code>myMatrix[0][0] = 1.0f;</code>  would set the first row, first column entry to <strong>1.0f</strong>.</p>
  
  <p>I'd like to provide similar access.  How can I overload the <code>[][] operator</code>s?</p>
  
  <p>I've tried the following, but I get errors:</p>
  
  <blockquote>
    <p>operator name must be declared as a function</p>
  </blockquote>
  
  <pre><code>const real operator[][](int row, int col) const
  {
      // should really throw an exception for out of bounds indices
      return ((row &gt;= 0 &amp;&amp; row &lt;= 2) &amp;&amp; (col &gt;= 0 &amp;&amp; col &lt;= 2)) ? _data[row][col] : 0.0f;
  }
  </code></pre>
  
  <p>What's the correct way to overload this operator?</p>