title: C++ - return pointer or const reference
body: <p>I have a Container that has a vector of objects. </p>
  
  <pre><code>class Container
  {
      vector&lt;Foo&gt; objects;
  </code></pre>
  
  <p>I want to</p>
  
  <ul>
  <li>Have read only access to these objects as a caller</li>
  <li>Avoid calling copy constructor</li>
  <li>Ignore the problem of accessing these objects after the container goes out of scope (this will never happen) </li>
  </ul>
  
  <p>Based on that, it seems like I have 2 choices for the getter method. Either return const reference to a vector, or return pointer to const vector</p>
  
  <pre><code>const vector&lt;Foo&gt; &amp; getRef() { return objects; }
  const vector&lt;Foo&gt; * getPtr() { return &amp;objects; }
  </code></pre>
  
  <p>Of course, if I'm the caller, I need to make sure to not call copy constructor when I iterate. So I'd this</p>
  
  <pre><code>for (const Foo &amp; f : getRef())
  </code></pre>
  
  <p>and not</p>
  
  <pre><code>for (Foo f : getRef())
  </code></pre>
  
  <p>Now, should I return const reference? or pointer to const? What's the benefits and drawbacks?</p>