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

  1. Path: sparky!uunet!paladin.american.edu!darwin.sura.net!mips!swrinde!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: <2284@devnull.mpd.tandem.com>
  6. Date: 21 Aug 92 02:28:26 GMT
  7. References: <1992Aug19.231926.28218@sunb10.cs.uiuc.edu> <2273@devnull.mpd.tandem.com> <matt.714318343@centerline.com>
  8. Sender: news@devnull.mpd.tandem.com
  9. Distribution: usa
  10. Organization: Tandem Computers, Micro-Products Division
  11. Lines: 74
  12.  
  13.  
  14. In article <matt.714318343@centerline.com> matt@centerline.com (Matt Landau) writes:
  15. >In <2273@devnull.mpd.tandem.com> rgp@mpd.tandem.com (Ramon Pantin) writes:
  16. >>>>Sure there is: Overload operator new()...
  17. >>>
  18. >>>*****>    Clever...but it's not reentrant.
  19. >
  20. >>Paul, that is a moot point given that the object might not always be
  21. >>created thru new.  All the flag setting/checking by Josh's overloaded
  22. >>new ends up being ignored for automatic objects (i.e. local variables).
  23. >>The fact that an object wants to allocate memory thru new for its internal
  24. >>use doesn't imply that the object itself will always be created thru new.
  25. >
  26. >Of course it's fairly straightforward to FORCE all objects of a given
  27. >class to be created via the class operator new (some some other delegated
  28. >creation function).  Simply design that class so that it has no public
  29. >constructors, and make the default constructor private.  The compiler
  30. >will now refuse to let either application-level code or subclasses call
  31. >any constructor for the class.  If you want subclasses to have direct
  32. >access to the constructors, make them protected instead of private.
  33.  
  34. Lets see if I follow the logic of this thread (very liberally paraphrased):
  35.  
  36. - Initial question:
  37.     How can I tell if there was an error during the execution
  38.     of a constructor?
  39.  
  40. - Initial reply:
  41.     You can't return a value in a constructor.  If you could do so,
  42.     then common idioms where a temporary object is created:
  43.         z = w * complex(real, imag);
  44.     could not be used.
  45.     You will have to wait until exception handling is available so
  46.     that the construction can throw an exception on error cases.
  47.  
  48. - Another reply (or what it boiled to):
  49.     You can tell if a memory allocation failed when you invoke new
  50.     (in that case the constructor is not called at all).
  51.  
  52. - From which followed:
  53.     Overload operator new and have some static variable thru which
  54.     the constructors and the overloaded new communicate so that it
  55.     can be checked by the overloaded new and a NULL returned by it
  56.     instead.
  57.   I'm not even sure that this can be done.  I have never overloaded new
  58.   but I thought that the overloaded new was used only to allocate the
  59.   memory (does it have to invoke the constructor by "hand"?), I'm not
  60.   sure at what point after the construction would the overloaded operator
  61.   new get control so that it can do the flag checking stuff.  Can somebody
  62.   follow up on this?
  63.  
  64. - Problem:
  65.     Automatic objects and arrays don't get their storage allocated
  66.     thru new.
  67.  
  68. - Latest (this) post:
  69.     Forbid the construction of automatic objects so that they have
  70.     to be always allocated thru new.
  71.  
  72.  
  73. So, to get an error code out of a constructor we should (note that I
  74. DON'T agree with all this!):
  75.  
  76. - Forbid the creation of automatic variables and arrays, i.e. make
  77.   the constructors non-public.
  78.  
  79. - Do the new/constructor thing.
  80.  
  81. - Have some public static member function that creates objects.
  82.  
  83. Sounds pretty convuluted, don't you think?
  84. No wonder nobody seems to write simple code anymore!
  85.  
  86. Ramon Pantin
  87.