title: Custom iterator out of bounds
body: <p>I have an iterator class. Let's call it <code>PIterator</code> here. A <code>MessageBuffer</code> is iterated and is being outputted correctly, unless the <code>nSizeOfMessage</code> plus where the iterator currently points to is equal to the size of the whole message (position correct, index one too large).</p>
  
  <p>If I check for the last element and decrement by one, it should work. Though it seems to be a "wrong way" to me. Yeah, I am not quite sure on this, so my problem is shown in this code snippet, maybe someone knows a good solution, tried to figure it out for quite a while.</p>
  
  <p>Yes, I do know how to use a debugger, I know where the problem lies and it is explained just fine. I do not know how to fix this, unless used the way I mentioned.</p>
  
  <p>This compiles fine under Visual Studio 2015.</p>
  
  <p>Please also see the comments in the main function.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class MessageBuffer
  {
  public:
      MessageBuffer(const std::string &amp;s)
      {
          _msgBuffer.assign(s.begin(), s.end());
      }
  
      char &amp;operator[](std::size_t nIndex)
      {
          return _msgBuffer[nIndex];
      }
  
      //more functions...
  
  private:
      std::vector&lt;char&gt; _msgBuffer;
  };
  
  class PIterator
  {
  public:
      PIterator(MessageBuffer &amp;b)
          : m_Ref(b)
          , m_Where(0)
      { }
  
      PIterator &amp;operator=(PIterator &amp;other)
      {
          if (this == &amp;other)
              return *this;
          this-&gt;m_Ref = other.m_Ref;
          this-&gt;m_Where = other.m_Where;
          return *this;
      }
  
      //more functions...
  
      PIterator operator+(unsigned int nValue) const
      {
          PIterator copy(*this);
          copy.m_Where += nValue;
          return copy;
      }
  
      PIterator &amp;operator+=(unsigned int nValue)
      {
          m_Where += nValue;
          return *this;
      }
  
      char &amp;operator*()
      {
          return m_Ref[m_Where];
      }
  
  private:
      MessageBuffer &amp;m_Ref;
      std::size_t m_Where;
  };
  
  int wmain(int argv, wchar_t **args)
  {
      std::string msg = "123MyMessage"; //Length 12
                                      //                    ^ Index 3, Position 4
  
      MessageBuffer mb(msg);
  
      PIterator itr(mb);
  
      //Calculations - here the results hardcoded
      std::size_t nSizeOfMessage = 9; //The size of the message without the numbers
  
                                      //itr.m_Where is 3 - That's where the non-numeric part of the message starts
      itr += 3;
  
      std::string needThis;
  
      PIterator cpy = itr + nSizeOfMessage; //itr points to the first element of the message
                                          //cpy is now out of bounds - position is correct, but index is 1 too large
  
      needThis.assign(&amp;*itr, &amp;*cpy); //boom
  
      return 0;
  }
  </code></pre>