>>>>> "Hyrum K. Wright" <hyrum_wri...@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))

Reply via email to