1

Possible Duplicate:
When to return a pointer, scalar and reference in C++?

Hi,

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

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

// 1.
Sample GetSample()
{
    Sample sample;
    // fill sample
    return sample;
}

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

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

    boost::optional<std::wstring> window_caption_;
    boost::optional<std::wstring> image_fs_name_,
        image_full_path_,
        resource_image_name_;
    boost::optional<boost::posix_time::ptime> probe_time_;
    HWND window_handle_;
};
Community
  • 1
  • 1
userbb
  • 2,148
  • 5
  • 30
  • 53
  • 2
    Please tell us what `Sample` is. – David Heffernan May 01 '11 at 17:03
  • I didn't understand your question. In your example, you are using a local copy inside `GetSample()` that is copied in `Sample sample = GetSample();` and then you push this into the `samples` queue. By doing `samples.pop()` you retrieve the oldest `Sample` element in the queue. If you want to know if dequeues are better than queues, that much depends on what you want to do with them. – CRM May 01 '11 at 17:27

0 Answers0