home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!devnull!rgp
- From: rgp@mpd.tandem.com (Ramon Pantin)
- Newsgroups: comp.lang.c++
- Subject: Re: Return value for the constructors.
- Message-ID: <2273@devnull.mpd.tandem.com>
- Date: 20 Aug 92 05:47:12 GMT
- References: <1992Aug19.163545.25066@sunb10.cs.uiuc.edu> <1992Aug19.175852.38459@watson.ibm.com> <1992Aug19.231926.28218@sunb10.cs.uiuc.edu>
- Sender: news@devnull.mpd.tandem.com
- Distribution: usa
- Organization: Tandem Computers, Micro-Products Division
- Lines: 33
-
- In article <1992Aug19.231926.28218@sunb10.cs.uiuc.edu> pjl@sparc10.cs.uiuc.edu (Paul Lucas) writes:
- >In <1992Aug19.175852.38459@watson.ibm.com> mittle@watson.ibm.com (Josh Mittleman) writes:
- >
- >>pjl@sparc10.cs.uiuc.edu (Paul Lucas) writes:
- >
- >>Sure there is: Overload operator new(). For example, give your class a
- >>static status flag. operator new() sets it to 1, then calls ::new() to
- >>allocate the object. Your constructors make whatever internal checks they
- >>need, and set status to 0 if there is a problem. Then, operator new()
- >>returns a pointer to the new object or 0, depending on the success of the
- >>process. From the users point-of-view, it simply looks like a normal call
- >>to new().
- >
- >*****> Clever...but it's not reentrant.
-
- Paul, that is a moot point given that the object might not always be
- created thru new. All the flag setting/checking by Josh's overloaded
- new ends up being ignored for automatic objects (i.e. local variables).
- The fact that an object wants to allocate memory thru new for its internal
- use doesn't imply that the object itself will always be created thru new.
- Similar arguments can be made for arrays of objects allocated thru new
- where the overloaded new does not end up being called (if I'm not
- mistaken).
-
- I have seen error codes being "returned" (ok, not "return" returned,
- but set thru a pointer) by constructors by passing the address of an
- integer to the constructor with the subsequent tests of this variable.
- I don't like this alternative very much because the object also
- needs to remember in most cases if he is ok or not so that its desctruction
- can act accordingly (in which case an "I'm ok" method could be provided
- just as easily).
-
- Ramon Pantin
-