home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12612 < prev    next >
Encoding:
Internet Message Format  |  1992-08-19  |  2.2 KB

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