Return as pointer, reference or object?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/3431484/when-to-return-a-pointer-scalar-and-reference-in-c">When to return a pointer, scalar and reference in C++?</a>  </p>
</blockquote>



<p>Hi,</p>

<p>In example 1. I don't know what to use. <code>Sample</code> should be returned as pointer or reference or like it is. Next, in example 2. <code>GetSample</code> is used to retrieve Sample object and pushed on queue. I don't know here if should i use <code>std::dequeue&lt;Sample*&gt;</code>. I see that object sample was copied 2 times (when returned from <code>GetSample</code> and when was pushed on queue). </p>

<p>How should it be if speed is not so important. I'am interested in best practice.</p>

<pre><code>// 1.
Sample GetSample()
{
    Sample sample;
    // fill sample
    return sample;
}

// 2.
class AAA
{
    void MakeSomethingWithSample()
    {
        Sample sample = GetSample();
        samples.push(sample);
    }
    std::dequeue&lt;Sample&gt; samples;
};

class Sample
{
public:
    Sample(void);
    ~Sample(void);

    boost::optional&lt;std::wstring&gt; window_caption_;
    boost::optional&lt;std::wstring&gt; image_fs_name_,
        image_full_path_,
        resource_image_name_;
    boost::optional&lt;boost::posix_time::ptime&gt; probe_time_;
    HWND window_handle_;
};
</code></pre>
