home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16515 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.3 KB  |  59 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!math.fu-berlin.de!fauern!ira.uka.de!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: question on REFERENCES
  5. In-Reply-To: kestes@nswc-wo.nswc.navy.mil's message of Tue, 17 Nov 1992 14:10:55 GMT
  6. Message-ID: <KANZE.92Nov18170843@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <1992Nov17.073603.5235@murdoch.acc.Virginia.EDU> <75096@hydra.gatech.EDU>
  10.     <1992Nov17.141055.19557@relay.nswc.navy.mil>
  11. Date: 18 Nov 92 17:08:43
  12. Lines: 45
  13.  
  14. In article <1992Nov17.141055.19557@relay.nswc.navy.mil>
  15. kestes@nswc-wo.nswc.navy.mil (Kent Estes) writes:
  16.  
  17. |> I am working on a function that references objects via id.  Upon finding
  18. |> an object, it returns a reference to that object.  My question is :
  19. |> if the object is not found, what is the best way to handle this.  If 
  20. |> I were returning pointers to objects, all I would have to do is
  21. |> return NULL.
  22.  
  23. Two possibilities:
  24.  
  25. Add the element being looked up to the set of objects, with a default
  26. value, and return this.  This is a good solution for associative
  27. arrays, for example, where the operator[] never fails.
  28.  
  29. Or treat such a reference as an error, and do your error handling
  30. thing.  Exceptions would be nice here, but while we're waiting, I
  31. generally use a call back function.  Note, however, that if the
  32. call-back function returns to where it was called from, you'd better
  33. have something to return anyway.  (A static value would do the trick.
  34. The important thing is that the user is supposed to do something in
  35. the call-back function so that he won't use the reference later
  36. anyway.)
  37.  
  38. To give a concrete example, from my assocative array class:
  39.  
  40.     class AssocArray
  41.     {
  42.     public :
  43.         DataType&    operator[]( const KeyType& ) ;
  44.         const DataType&    operator[]( const KeyType& ) const ;
  45.         int        contains( const KeyType& ) const ;
  46.     } ;
  47.  
  48. The first operator[] uses the first solution.  The second operator[],
  49. which will be used on constant objects, uses the second solution, with
  50. a default call-back function which aborts the program with an error
  51. message.  The contains function is there so that the user can find out
  52. whether the key is present *before* creating it (in a non-const
  53. object) or calling the call-back function (in a const object).
  54. --
  55. James Kanze            GABI Software, Sarl.
  56. email: kanze@us-es.sel.de    8 rue du Faisan
  57.                 67000 Strasbourg
  58.                 France
  59.