From: swillden@news.ccutah.edu (Shawn Willden)
Subject: Re: question on REFERENCES
Organization: University of Utah Computer Center
X-Newsreader: Tin 1.1 PL3
Date: Wed, 18 Nov 92 17:24:28 GMT

jbn@lulea.trab.se (Johan Bengtsson) writes:
: swillden@news.ccutah.edu (Shawn Willden) writes:
: : kestes@nswc-wo.nswc.navy.mil (Kent Estes) writes:
: : : 
: : : I am working on a function that references objects via id.  Upon finding
: : : an object, it returns a reference to that object.  My question is :
: : : if the object is not found, what is the best way to handle this.  If 
: : : I were returning pointers to objects, all I would have to do is
: : : return NULL.
: : 
: : So return a pointer.  There is no way to return a NULL reference (which
: : is good, if you have a reference you *know* that it refers to something).
: : 
: : If there really is a good reason why you need to return a reference 
: : instead of a pointer you can declare a special object of the return
: : type that is always to be returned in case of error and then compare
: : the address of the returned object with the address of the error 
: : object (or overload operator== and play games with that).  Usually,
: : though, you're better off just returning a pointer.
: 
: I disagree.  The described scheme requires the user to test
: the result of every function call that may fail.  This is error prone,
: and in the case of NULL pointers causes program termination (on
: many machines), if overlooked.  Also, the code tends to be severely
: cluttered with tests for NULL.

Agreed.

: A better approach (IMHO) is to register errors in an error variable,
: which can be a global variable, or one per object (for member functions).
: The error variable must be explicitly read and reset, before proceeding.

How is this different from a test for NULL?  The syntax may be slightly
cleaner but not much is really gained by this approach.  Something must
still be returned and that something will be invalid if the function 
fails.  Why is the programmer more likely to remember to test an error
flag than to test a returned pointer against NULL?  It can be argued 
that a program should halt for a serious programmer error such as this
so as to help the debugging process.  There is one advantage to an error 
flag, of course.  You can provide the user with information about why 
the function failed which is useful if there is more than one possible 
reason for failure.  Where this is true I would recommend your approach.

: The iostream library uses this approach, guaranteeing that all operations
: on a stream are ignored until the error is fixed.  The advantage is

Where applicable, this is probably the best way (barring exceptions,
which few of us have -- yet).  It ensures that nothing extremely bad
will happen until the error status is cleared.  In the case of a 
function that returns an object, however, how do you prevent the 
object returned from being used?  You must include a flag in the object
that indicates the goodness of the state of the object.  This is 
reasonable in some cases, not in others, depending on how serious
the failure is (i.e. maybe the error is such that even an invalid
object cannot be constructed).  In addition, it requires all code
that uses an object to test its goodness and then take appropriate
action if not.  In many cases it is more convenient to design
the object such that a given instance is always valid and to just avoid
creating invalid ones.

: that it is OK to omit some error tests, several function calls may
: safely be made before testing for errors.

Many times beginning C++ programmers get the idea that pointers are
the `C' way to do things and references are the `C++' way (implying
that pointers should not be used in `real' C++ code).  I was trying
to point out to the original poster that pointers have their place.

: Of course, true C++ exceptions is the superior alternative (:-).
 
Someday...

: -- 
: --------------------------------------------------------------------------
: | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
: | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
: --------------------------------------------------------------------------

--
Shawn Willden
swillden@icarus.weber.edu