Getting Record entries I have a class Record which keeps entries of strings(implemented using vector of strings). I want to keep the data member private to the class. To get entries, I have defined a member function get_entry which accepts a reference to vector of strings to store results.

Below is the code.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

class Record {
public:
        void add_entry(const std::string&amp; entry)
        {
                static int seq = 1;
                entries.push_back(std::to_string(seq++) + " "+ entry);
        }
        void get_entry(std::vector&lt;std::string&gt;&amp; res) const
        {
                for (auto &amp;v: entries)
                        res.push_back(v);
        }

private:
        std::vector&lt;std::string&gt; entries;
};

int main()
{
        Record name;
        name.add_entry("abc");
        name.add_entry("def");
        std::vector&lt;std::string&gt; res;
        name.get_entry(res);
        for (auto &amp;v : res)
                std::cout &lt;&lt; v &lt;&lt; "\n";
        return 0;
}


Are there other better ways to get entries from such a class? I don't want to make entries data member public and access it directly.
The obvious downside of your current approach is that you do a lot potentially unnecessary copying. One common solution is to return (const) iterators to the entries, i.e.,:

class Record
{
    public:
        // ...

       auto cbegin() const { return entries.cbegin(); }
       auto cend() const { return entries.cend(); }

       // Perhaps similar methods for getting non-const iterators ...
};


This solution allows you to use e.g., standard algorithms on the entries, but you can't manipulate the underlying range from the outside (due to const-ness).

To support your earlier use case, you can still do std::vector&lt;std::string&gt; res(r.cbegin(), r.cend()); if you wish, where r is an object of type Record.