title: C++: Reference to dynamic memory
body: <p>All considerations about when to use which aside, I am still unsure about pointer vs reference semantics.</p>
  
  <p>Right now, I am under the impression that references are essentially pointers that must be initialized when they are declared, and then from that point on cannot point to anything else. In other words, they are a like a <code>Type* const</code> (not <code>Type const*</code>), or, they cannot be <em>reseated</em>. It essentially becomes a "new name" for that object. Now I heard that references do not actually need to be implemented by the compiler using pointers, but I am under the impression that you can still think of them this way, in regards to what their visible behavior will be.</p>
  
  <p>But why can't you do something like this:</p>
  
  <pre><code>int&amp; foo = new int;
  </code></pre>
  
  <p>I want to create a reference to dynamic memory. This does not compile. I get the error</p>
  
  <pre><code>error: invalid initialization of non-const reference of type 'int&amp;' from a temporary of type 'int*'
  </code></pre>
  
  <p>That makes sense to me. It seems the new operator returns a pointer of given type to the address of memory that the OS? dynamically allocated for me.</p>
  
  <p>So how do I create a "reference" to dynamic memory?</p>
  
  <p>Edit: Links to resources that precisely explain the difference between references and pointers in C++ would be appreciated.</p>