[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: object-model: Return by value, reference or pointer? (or something else?)

From: Steinar Bang <sb_at_dod.no>
Date: Thu, 14 Oct 2010 10:37:34 +0200

>>>>> "Hyrum K. Wright" <hyrum_wright_at_mail.utexas.edu>:

> I don't want the caller to have to depend upon the lifetime of the
> source object, hence the desire to return something by value or a
> newly allocated pointer.

> Additionally, it still wouldn't work, since references have to point
> to some object, hence there is no such thing as a "null reference".

Well... this might actually be a use for auto_ptr<string>.

If you return auto_ptr<string> you force the caller to assign it to an
auto_ptr<string> (ie. something that will clean up after itself when it
goes out of scope). This assignment would assign ownership to the
string from the return-by-value'd auto_ptr<string> to the variable you
assign it to.

If you fail to assign, the returned auto_ptr should go out of scope, and
delete the string it returns to.

That is... if auto_ptr<string> can hold NULL values. I don't know if it
can (I think it can, but I've never used it, so I don't know). Yep,
looks like it does. See eg. http://en.wikipedia.org/wiki/Auto_ptr
(example program with assignment a bit down).

> (This entire conversation is reminding my why I *hate* C++. To bad
> there isn't too much of an alternative here... :/)

(I saw a review once for Scott Meyers' "Effective C++", where the
reviewer said something like "Scott Meyers shows that he has a thorugh
grasp of C++. It can be debated whether having a through grasp of C++
is a good thing or not...". :-) (btw if you have to work in C++ then
that particular book, is essential reading))
Received on 2010-10-14 10:38:28 CEST

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.