title: std vector const_iterator assignment Access violation
body: <p>I'm working on code in which 1 of more variable length object are stored as a vector of all units i.e. assuming each letter is a unit, and the same letter are the same high level object.
  the vector may contain something like the following: aaaabbcccdeeffff...</p>
  
  <p>This vector is an internal private variable of a class which has a operator[] defined such that for the vector above</p>
  
  <pre><code> - obj[0] returns an const_iterator to the first "a"
   - obj[1] to the first "b"
   - obj[2] to the first "c" etc.
  
  const AI::GeneArray::const_iterator&amp; AI::Chromosome::operator[](int index) const {
      GeneArray::const_iterator pGene;
      int cIndex;
      for (cIndex=0,pGene = this-&gt;vGenes.cbegin();pGene != this-&gt;vGenes.cend();pGene++, cIndex++) {
          if (index == cIndex) { break; }
          (*pGene)-&gt;EndOfBlock(pGene); }
      return pGene; }
  </code></pre>
  
  <p>Then in another function I have the following</p>
  
  <pre><code>AI::GeneArray::const_iterator function = vChromosome[0];
  </code></pre>
  
  <p>This causes an Access violation Error.</p>
  
  <p>The Call stack's above my function are as follows</p>
  
  <pre><code>AI.exe!std::_Vector_const_iterator&lt;std::_Vector_val&lt;AI::Gene *,std::allocator&lt;AI::Gene *&gt; &gt; &gt;::_Vector_const_iterator&lt;std::_Vector_val&lt;AI::Gene *,std::allocator&lt;AI::Gene *&gt; &gt; &gt;(const std::_Vector_const_iterator&lt;std::_Vector_val&lt;AI::Gene *,std::allocator&lt;AI::Gene *&gt; &gt; &gt; &amp; __that)  + 0x2f byte
  AI.exe!std::_Iterator012&lt;std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &amp;,std::_Iterator_base12&gt;::_Iterator012&lt;std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &amp;,std::_Iterator_base12&gt;(const std::_Iterator012&lt;std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &amp;,std::_Iterator_base12&gt; &amp; __that)  + 0x2f bytes
  AI.exe!std::_Iterator_base12::_Iterator_base12(const std::_Iterator_base12 &amp; _Right)  Line 118
  AI.exe!std::_Iterator_base12::operator=(const std::_Iterator_base12 &amp; _Right)  Line 123 + 0x5 bytes
  </code></pre>
  
  <p>The final Call is to </p>
  
  <pre><code>_Iterator_base12&amp; operator=(const _Iterator_base12&amp; _Right)
      {   // assign an iterator
          if (_Myproxy != _Right._Myproxy)
              _Adopt(_Right._Myproxy-&gt;_Mycont);&lt;- This line
          return (*this);
      }
  </code></pre>
  
  <p>According to my debugger (Visual C++ 2010 Express)</p>
  
  <pre><code>_Right._Myproxy-&gt;
     _Mycont = CXX0030: Error: expression cannot be evaluated
     _Myfirstiter = CXX0030: Error: expression cannot be evaluated
  </code></pre>
  
  <p>Else where in my project I have similar code using std::list, rather than vector that works correctly</p>
  
  <pre><code>parents = new ChromosomeList::const_iterator[C];
  *(parents) = --(this-&gt;vChromosomes.cend());
  for (int i=1;i&lt;C;i++) {
      *(parents+i) = ChromosomeList::const_iterator((*(parents+i-1)));
      (*(parents+i))--; }
  </code></pre>
  
  <p>I've checked the value returned by</p>
  
  <p>vChromosome[0]</p>
  
  <p>this value is of the correct type,</p>
  
  <pre><code>const AI::GeneArray::const_iterator &amp;
  </code></pre>
  
  <p>I've search Google for similar problems, All I've been able to find is problem related to looping through a vector using iterator's</p>
  
  <p>i.e.</p>
  
  <pre><code>for (AI::GeneArray::const_iterator pGene = Genes.cbegin();pGene != Genes.cend();pGene++)
  </code></pre>
  
  <p>Such Code in my project is working correctly.</p>