[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

return value

jerry

11/6/2008 8:03:00 AM

i want to return an empty SET object,i write it as "return set<string>
temp",but there are some error when complie it,and i have told that i
should write it as "return set<string> temp()",who can
tell me why?
thank you!
1 Answer

SG

11/6/2008 9:08:00 AM

0

On 6 Nov., 09:02, jerry <machel2...@yahoo.cn> wrote:
> i want to return an empty SET object,i write it as "return set<string>
> temp",but there are some error when complie it,and i have told that i
> should write it as "return set<string> temp()",who can
> tell me why?
> thank you!

return set<string>(); // default constructed temporary

You should be aware of that returning containers that way may be
expensive because all the container's entries are copied. If you don't
want this try one of these alternatives:

* add a function parameter: a pointer or reference to a set where
the result should be stored into and make the function void.
* create the set on the heap, put the pointer into a std::auto_ptr,
and return the auto_ptr object (not very popular these days, I
think)
* wait for an implementation of the upcoming C++ standard
(move semantics)

Cheers,
SG