title: Returning a reference to an element of a class member container
body: <p>Foo behaves like a circular iterator.  Despite me being nervous about it, the code below compiles fine, but creates a run-time error.  I receive the error even if I remove the consts from get_current().  Of course, I can return a pointer and it'll work; however, will I get better security returning a reference?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;array&gt;
  #include &lt;memory&gt;
  
  class Foo
  {
  public:
    Foo();
    void next();
    const int&amp; get_current() const;
  private:
    std::array&lt;std::unique_ptr&lt;int&gt;, 3&gt; arr_;
    unsigned i_;
  };
  
  Foo::Foo() : i_(0)
  {
    arr_[0] = std::unique_ptr&lt;int&gt;(new int(5));
    arr_[1] = std::unique_ptr&lt;int&gt;(new int(6));
    arr_[2] = std::unique_ptr&lt;int&gt;(new int(7));
  }
  
  void Foo::next()
  {
    ++i_;
    i_ %= 3;
  }
  
  const int&amp; Foo::get_current() const 
  {
    return *arr_[i_];
  }
  
  int main()
  {
    Foo foo;
    int* p;
  
    *p = foo.get_current();
    //do something with p
    std::cout &lt;&lt; *p &lt;&lt; std::endl;
  
    foo.next();
    *p = foo.get_current();
    //do something with p
    std::cout &lt;&lt; *p &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>