home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!ukma!asuvax!ncar!csn!cns!donley
- From: donley@cscns.com (Donley P'Simer)
- Subject: Re: question on REFERENCES
- Message-ID: <BxvKIE.5Mt@cscns.com>
- Organization: Community_News_Service
- X-Newsreader: Tin 1.1 PL3
- References: <1992Nov17.141055.19557@relay.nswc.navy.mil>
- Date: Tue, 17 Nov 1992 19:23:02 GMT
- Lines: 49
-
- kestes@nswc-wo.nswc.navy.mil (Kent Estes) writes:
- : Hello netters,
- :
- : 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.
- :
-
- The way that Borland's Container Class Library does it seems the best way
- to me. All BI's container classes are derived from Object. There is a
- special instantiation of Object called ZERO. If you do something similiar
- you can return a reference to your special error object and even store
- error infomation in that object. The user must test all returned
- references against this object. When an error is detected the user can
- extract error information from it. The following code should give you an
- idea of what you can do:
-
- class Object
- {
- // your data
- public:
- // constuctors, destuctors, and the like.
- int foo() { if (/* there is an error */) return 0; else return 1;}
- };
-
- Object ERROR;
-
- Object& bar()
- {
- Onject &O;
-
- /* find an object and reference it by O */
- if !O.foo()
- return ERROR;
- else
- return O;
- }
-
- This is totally unfunctional, but I think you can get the picture. I hope
- this helps.
-
-
- --
- {-| Donley R. P'Simer |-}
-
- Internet: donley@cscns.com PSC Box 367
- CompuServe: 75540,263 Peterson AFB, CO. 80914-5360
-