Actually, (IMO) method 1 looks more readable than the second.
You shouldn't think about a solution which would be the most efficient in all cases
eg:
1 2
|
struct myAggregateType1 { int x, y; };
typedef vector<vector<int> > myAggregateType2;
| |
myAggregateType1 is very cheap to copy and myAggregateType2 is not.
If you have a function which builds some object and has to return it, the most intuitive way is to return it by value.
If that object is too large, the simplest option is to modify a reference passed to the function ( so you don't have to manage memory allocation/deallocation )
Let's examine your idea of returning an auto_ptr:
1 2 3 4
|
auto_ptr<huge_type> build_huge_object()
{
return auto_ptr<huge_type> ( new huge_type );
}
| |
The first thing is that auto_ptr will soon be deprecated so it won't be quite a good choice
Someone using a function called
build_huge_object ( which has the job to create an object of
huge_type ) returning an auto_ptr is counter-intuitive.
Returning a plain pointer to
huge_type would be more intuitive but it will require to keep track of that object and delete it when not needed any more
PS: Please use format tags
http://www.cplusplus.com/articles/firedraco1/