title: Returning different iterators with virtual derived methods
body: <p>Say I have a virtual base class <code>Base</code>, which will in part behave like a container, with two derived classes <code>VectorLike</code> and <code>RangeLike</code>.
  I want to achieve something like the following:</p>
  
  <pre><code>class VectorLike : public Base {
      std::vector&lt;int&gt; data;
  public:
      virtual std::vector&lt;int&gt;::const_iterator cbegin() { return data.cbegin() }
      virtual std::vector&lt;int&gt;::const_iterator cend() { return data.cend() }
  }
  
  class RangeLike : public Base {
      int min, max;
      class const_iterator {
          int x;
      public:
          int operator++() { return ++x }
          bool operator==( const_iterator rhs ) { return x == rhs.x }
          const_iterator( int y ) { x = y }
      }
  public:
      virtual const_iterator cbegin() { return const_iterator( min ); }
      virtual const_iterator cend() { return const_iterator( max ); }
  }
  </code></pre>
  
  <p>This code will not compile, since <code>std::vector&lt;int&gt;::const_iterator</code> and <code>RangeLike::const_iterator</code> aren't identical or <a href="http://www.lwithers.me.uk/articles/covariant.html" rel="noreferrer">covariant</a>.
  To achieve the second, I would need an iterator base class from which both <code>std::vector&lt;int&gt;::const_iterator</code> and <code>RangeLike::const_iterator</code> will derive. But then still <code>cbegin()</code> and <code>cend()</code> will have to return pointers to iterators, which will make an even bigger mess. </p>
  
  <p>My question is, is it possible to achieve something like the above code and if so how?</p>