title: C++ iterator and const_iterator problem for own container class
body: <p>I'm writing an own container class and have run into a problem I can't get my head around. Here's the bare-bone sample that shows the problem.</p>
  <p>It consists of a container class and two test classes: one test class using a std:vector which compiles nicely and the second test class which tries to use my own container class in exact the same way but fails miserably to compile.</p>
  <pre><code>#include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;iterator&gt;
  
  using namespace std;
  
  template &lt;typename T&gt;
  class MyContainer
  {
  public:
    
    class iterator
    {
    public:
      typedef iterator self_type;
      inline iterator() { }
    };
  
    class const_iterator
    {
    public:
      typedef const_iterator self_type;
      inline const_iterator() { }
    };
    
    iterator begin() {
      return iterator();
    }
  
    const_iterator begin() const {
      return const_iterator();
    }
  };
  
  // This one compiles ok, using std::vector
  class TestClassVector
  {
  public:
    void test() {
      vector&lt;int&gt;::const_iterator I=myc.begin();
    }
  
  private:
    vector&lt;int&gt; myc;
  };
  
  // this one fails to compile. Why?
  class TestClassMyContainer
  {
  public:
    void test(){
      MyContainer&lt;int&gt;::const_iterator I=myc.begin();
    }
  
  private:
    MyContainer&lt;int&gt; myc;
  };
  
  
  int main(int argc, char ** argv)
  {
    return 0;
  }
  </code></pre>
  <p>gcc tells me:</p>
  <blockquote>
  <p>test2.C: In member function ‘void TestClassMyContainer::test()’:</p>
  <p>test2.C:51: error: conversion from ‘MyContainer::iterator’ to non-scalar type ‘MyContainer::const_iterator’ requested</p>
  </blockquote>
  <p>I'm not sure where and why the compiler wants to convert an iterator to a const_iterator for my own class but not for the STL vector class. What am I doing wrong?</p>