title: Properly overloading [bracket] operator for hashtable get and set
body: <p>I am trying to implement a hashtable class. The problem I am facing atm is how to properly overload the square bracket operators so that getting the value at a key from the hashtable is distinguishable from setting a key to a value.</p>
  <p>So far here is what the class looks like:</p>
  <pre><code>template &lt;typename K, typename V&gt;
  class HashTable {
      typedef pair&lt;K, V&gt; KeyVal;
      avl_tree &lt;KeyVal&gt; **TABLE;
      unsigned TABLESIZE;
      
  public:
      HashTable( const unsigned &amp; );
      V&amp; operator [] ( const K&amp; ); //Setter
      const V&amp; operator [](const K&amp;) const; //Getter
      typedef unsigned (*hashtype)(const K&amp;);
      static hashtype Hash;
      ~HashTable();
  };
  </code></pre>
  <p>And this is the implementation of each overload of the brackets:</p>
  <pre><code>template &lt;typename K, typename V&gt;
  V&amp; HashTable&lt;K, V&gt;::operator [] ( const K&amp; ret ) {
      unsigned index = HashTable&lt;K, V&gt;::Hash(ret) % TABLESIZE;
      avl_tree &lt;KeyVal&gt; *ptr = AVL_TREE::find(TABLE[index], KeyVal(ret, 0));
      if ( ptr == None ) ptr = (TABLE[index] = AVL_TREE::insert(TABLE[index], KeyVal(ret, 0)));
      return ptr-&gt;data.second;
  }
  
  template &lt;typename K, typename V&gt;
  const V&amp; HashTable&lt;K, V&gt;::operator [](const K&amp; ret) const {
      avl_tree &lt;KeyVal&gt; *ptr = AVL_TREE::find(TABLE[HashTable&lt;K, V&gt;::Hash(ret) % TABLESIZE], KeyVal(ret, 0));
      if (ptr == None) throw &quot;Exception: [KeyError] Key not found exception.&quot;;
      return ptr-&gt;data.second;
  }
  </code></pre>
  <p>Now if I do:</p>
  <pre><code>cout &lt;&lt; table[&quot;hash&quot;] &lt;&lt; &quot;\n&quot;; //table declared as type HashTable&lt;std::string, int&gt;
  </code></pre>
  <p>I get an output of 0, but I want it to use the getter implementation of the overloaded square brackets; i.e. this should throw an exception. How do I do this?</p>