Re: What's your preferred way of returning a list of items?
On May 12, 2:21 pm, Sousuke <s0s...@gmail.com> wrote:
On May 12, 3:18 am, DeMarcus <use_my_alias_h...@hotmail.com> wrote:
[...]
// Returning a copy.
std::vector<A> getList() { return aList; }
void getList( std::vector<A>& v )
{
std::copy( aList.begin(), aList.end(), v.begin() );
Isn't that just a more complicated and unsafe way of doing:
v = aList;
?
The two don't have the same semantics. At all.
}
void getList( std::vector<A>* v )
{
std::copy( aList.begin(), aList.end(), v->begin() );
}
I recently started a thread on that subject (whether "out"
parameters should be pointers instead of references):
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/697...
It seems most agreed that it's a dumb guideline.
I don't see where you got that. It's an arbitrary guideline, in
the sense that there is no right answer. Different companies
will use different guidelines---there are pros and cons for
each.
[...]
Do you know more ways to return a list? What's your preferred way to
return a list of items?
Either "vector<A> getList()" or "void getList(vector<A>&)" if I need
copying, or "const vector<A>& getList() const" (and possibly a non-
const overload) if the returned object is a member variable.
The only "clean" solution is "vector<A> getList()".
Regretfully, it can have significant overhead if the list
contains a lot of elements, and the function is called in a
tight loop. In which case, you choose among a number of other
solutions, depending on the context and the local coding
guidelines.
--
James Kanze