title: Custom iterator operator overloading
body: <p>I am trying to implement <code>-&gt;</code> operator of a custom Iterator. However I am not getting how to define them precisely.</p>
  
  <p>My Iterator class and MapNode are defined like:</p>
  
  <pre><code>template &lt;typename Key_T,typename Mapped_T&gt;
  class Map&lt;Key_T,Mapped_T&gt;::Iterator
  {
      MapNode&lt;Key_T,Mapped_T&gt;* curr;
  }
  
  template &lt;typename Key_T,typename Mapped_T&gt;
  class MapNode
  {
      Key_T key;
      Mapped_T value;
      MapNode *up,*down,*prev,*next;
      friend class Map&lt;Key_T,Mapped_T&gt;;
  };
  </code></pre>
  
  <p>Now I want to overload operator->, but the problem is I am not exactly getting how to return pointer of pair of key and value where iterator is currently pointing:</p>
  
  <p>My current implementation is :</p>
  
  <pre><code>template &lt;typename Key_T,typename Mapped_T&gt;
  std::pair&lt;const Key_T, Mapped_T&gt;*
  Map&lt;Key_T,Mapped_T&gt;::Iterator::operator-&gt;() const
  {
      const Key_T currKey = (this-&gt;curr)-&gt;key;
      const Mapped_T currVal = (this-&gt;curr)-&gt;value;
  
      ValueType* vt = new ValueType(std::make_pair(currKey,currVal));
  
      return vt;
  }
  </code></pre>
  
  <p>But I am afraid that this will cause memory leaks as the ValueType pointer memory won't be deallocated ever.</p>
  
  <p>Can someone guide me on how can this be done correctly?</p>
  
  <p>Please help.</p>
  
  <pre><code>[ValueType is defined as std::pair&lt;const Key_T, Mapped_T&gt;]
  </code></pre>