title: Return C++ iterator reference
body: <h2>Example in which it does work as expected</h2>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  struct MyClass{
  
  
    const std::vector&lt;float&gt;::iterator&amp; begin(){
      return myvec.begin();
    }
  
    const std::vector&lt;float&gt;::iterator&amp; end(){
      return myvec.end();
    }
    std::vector&lt;float&gt; myvec;
  };
  
  int main(){
  
    std::vector&lt;float&gt; mainvec(8,0);
  
    MyClass myClass;
  
    myClass.myvec = mainvec;
  
    for (std::vector&lt;float&gt;::iterator it = myClass.begin();
        it != myClass.end();++it){
      std::cout &lt;&lt; *it &lt;&lt; " " ;
    }
  
    std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>In this code I get the following output:</p>
  
  <pre><code>0 0 0 0 0 0 0 0 
  </code></pre>
  
  <h2>An example which does NOT works as expected:</h2>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  struct MyClass{
  
  
    const std::vector&lt;float&gt;::iterator&amp; begin(){
      return myvec.begin();
    }
  
    const std::vector&lt;float&gt;::iterator&amp; end(){
      return myvec.end();
    }
    std::vector&lt;float&gt; myvec;
  };
  
  int main(){
  
    std::vector&lt;float&gt; mainvec(8,0);
  
    MyClass myClass;
  
    myClass.myvec = mainvec;
  
    const std::vector&lt;float&gt;::iterator&amp; end_reference = myClass.end();
  
    for (std::vector&lt;float&gt;::iterator it = myClass.begin();
        it != end_reference;++it){
      std::cout &lt;&lt; *it &lt;&lt; " " ;
    }
  
    std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>In this code I get the following output:</p>
  
  <pre><code>"empty output"
  </code></pre>
  
  <h2>The first code example</h2>
  
  <p>It has the problem in which I invoke (erroneously) the vector <code>begin()</code> and <code>end()</code> instead of MyClass methods.</p>
  
  <p>I have the following minimal code to represent my doubt:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  struct MyClass{
  
  
    const std::vector&lt;float&gt;::iterator&amp; begin(){
      return myvec.begin();
    }
  
    const std::vector&lt;float&gt;::iterator&amp; end(){
      return myvec.end();
    }
    std::vector&lt;float&gt; myvec;
  };
  
  int main(){
  
    std::vector&lt;float&gt; mainvec(8,0);
  
    MyClass myClass;
  
    myClass.myvec = mainvec;
  
    for (std::vector&lt;float&gt;::iterator it = myClass.myvec.begin();
        it != myClass.myvec.end();++it){
      std::cout &lt;&lt; *it &lt;&lt; " " ;
    }
  
    std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>I get the following warnings on lines 8 and 12:</p>
  
  <pre><code>returning reference to local temporary object [-Wreturn-stack-address] [cpp/gcc] 
  </code></pre>
  
  <p>but when I compile and run the program, I get:</p>
  
  <pre><code>0 0 0 0 0 0 0 0 
  </code></pre>
  
  <p>So, it seems that the local reference is not destroyed when it returns the <code>myvec.begin()</code>. When I first wrote the code, I didn't thought it was going to be a problem, as in my head the begin() method from the vector would return a iterator reference to the first vector place, this iterator for me is not being allocated when I do myvec.begin(), but is a reference to this iterator. So, this warning shouldn't appear, as I am not allocating memory. But since I don't know how this mechanism works, I would like to learn it for writing consistent code. It seems that I can ignore this warning, can't I? </p>