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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ukma!asuvax!ncar!csn!cns!donley
  3. From: donley@cscns.com (Donley P'Simer)
  4. Subject: Re: question on REFERENCES
  5. Message-ID: <BxvKIE.5Mt@cscns.com>
  6. Organization: Community_News_Service
  7. X-Newsreader: Tin 1.1 PL3
  8. References: <1992Nov17.141055.19557@relay.nswc.navy.mil>
  9. Date: Tue, 17 Nov 1992 19:23:02 GMT
  10. Lines: 49
  11.  
  12. kestes@nswc-wo.nswc.navy.mil (Kent Estes) writes:
  13. : Hello netters,  
  14. : I am working on a function that references objects via id.  Upon finding
  15. : an object, it returns a reference to that object.  My question is :
  16. : if the object is not found, what is the best way to handle this.  If 
  17. : I were returning pointers to objects, all I would have to do is
  18. : return NULL.
  19.  
  20. The way that Borland's Container Class Library does it seems the best way
  21. to me. All BI's container classes are derived from Object. There is a
  22. special instantiation of Object called ZERO. If you do something similiar
  23. you can return a reference to your special error object and even store
  24. error infomation in that object. The user must test all returned
  25. references against this object. When an error is detected the user can
  26. extract error information from it. The following code should give you an
  27. idea of what you can do:
  28.  
  29. class Object
  30. {
  31.     // your data
  32. public:
  33.     // constuctors, destuctors, and the like.
  34.     int foo() { if (/* there is an error */) return 0; else return 1;}
  35. };
  36.  
  37. Object ERROR;
  38.  
  39. Object& bar()
  40. {
  41.     Onject &O;
  42.  
  43.     /* find an object and reference it by O */
  44.     if !O.foo()
  45.     return ERROR;
  46.     else
  47.     return O;
  48. }
  49.  
  50. This is totally unfunctional, but I think you can get the picture. I hope
  51. this helps.
  52.  
  53.  
  54. -- 
  55.             {-| Donley R. P'Simer |-}
  56.  
  57. Internet: donley@cscns.com        PSC Box 367
  58. CompuServe: 75540,263            Peterson AFB, CO. 80914-5360
  59.