Best way to return an object in c++?
<p>I'm pretty noobish when it comes to c++, what is the better way of returning an object?  I'm coming from the scripting world where Objects are always references, and am trying to achieve the same notion ... I'm basing this off of <a href="http://stackoverflow.com/questions/3613065/when-to-pass-by-reference-and-when-to-pass-by-pointer-in-c">When to pass by reference and when to pass by pointer in C++?</a>, where one user stated: "A good rule of thumb: "Use references when you can and pointers when you have to"."</p>

<pre><code>// basic layer class
class Layer { private: Channel channel; // NEVER NULL };

// return object by pointer
Channel *Layer::getChannel() {
    return &amp;channel;
};

// return by reference
Channel&amp; Layer::getChannel() {
    return channel;
};
</code></pre>

<p>The problem with the second version is that the compiler will accept this line:</p>

<pre><code>Channel channel = layer.getChannel();  // creates a copy BAD
</code></pre>

<p>when it should be:</p>

<pre><code>Channel &amp;channel = layer.getChannel();  // reference good
</code></pre>

<p>Is there any way to enforce a caller of the second option to force it to not create a new channel, or is the first option better anyways, even if it will never be NULL?</p>
