title: C++ , how to return a reference to the member variable from iterator
body: <p>There is the following class B derived from std::vector</p>
  
  <pre><code>class B: public std::vector &lt;unsigned int&gt;
  {
      public:
              B() : std::vector &lt;unsigned int&gt; ( 0 ) {}
  };
  </code></pre>
  
  <p>and a class A witten as follows:</p>
  
  <pre><code>class A
  {
      private:        
              B b;    
      double x;
  
  public:
      B &amp; getB () {return b;}
      B const &amp; getB() const {return b;}
  
      bool operator() ( const A &amp; a ) const
              {
                      return a &lt; a.x;
              }
  };
  </code></pre>
  
  <p>Why it is impossible to return a refererence to the variable b of some object A stored in std::list from its iterator (and how to do that)?</p>
  
  <pre><code>int main ()
  {
  std::set &lt;A &gt; alist;
  std::set &lt;A&gt; ::iterator i_alist = alist.begin();
  
  for (; i_alist != alist.end(); i_list++)
  {
      B &amp;il = (*i_alist).getB(); //Compiler error
      B &amp;il2 = i_alist-&gt;getB(); //Compiler error
  
      il.push_back(10);   //Modify il and concurrently B
  }
  } 
  </code></pre>
  
  <p>Compiler error:</p>
  
  <pre><code>Error   1   error C2440: 'initializing' : cannot convert from 'const B' to 'B &amp;'    d:\test.cpp
  </code></pre>
  
  <p>Thanks for your help...</p>
  
  <p><strong>Edit question</strong>:</p>
  
  <p>The possible solution using const_cast :</p>
  
  <pre><code>B &amp;il2 = const_cast &lt;B&amp;&gt; ( i_alist-&gt;getB() );
  </code></pre>