home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12639 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.0 KB  |  41 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!centerline!matt
  3. From: matt@centerline.com (Matt Landau)
  4. Subject: Re: Return value for the constructors.
  5. Message-ID: <matt.714318343@centerline.com>
  6. Sender: news@centerline.com
  7. Nntp-Posting-Host: rapier
  8. Organization: CenterLine Software, Inc.
  9. References: <1992Aug19.163545.25066@sunb10.cs.uiuc.edu> <1992Aug19.175852.38459@watson.ibm.com> <1992Aug19.231926.28218@sunb10.cs.uiuc.edu> <2273@devnull.mpd.tandem.com>
  10. Distribution: usa
  11. Date: Thu, 20 Aug 1992 13:45:43 GMT
  12. Lines: 27
  13.  
  14. In <2273@devnull.mpd.tandem.com> rgp@mpd.tandem.com (Ramon Pantin) writes:
  15. >>>Sure there is: Overload operator new()...
  16. >>
  17. >>*****>    Clever...but it's not reentrant.
  18.  
  19. >Paul, that is a moot point given that the object might not always be
  20. >created thru new.  All the flag setting/checking by Josh's overloaded
  21. >new ends up being ignored for automatic objects (i.e. local variables).
  22. >The fact that an object wants to allocate memory thru new for its internal
  23. >use doesn't imply that the object itself will always be created thru new.
  24.  
  25. Of course it's fairly straightforward to FORCE all objects of a given
  26. class to be created via the class operator new (some some other delegated
  27. creation function).  Simply design that class so that it has no public
  28. constructors, and make the default constructor private.  The compiler
  29. will now refuse to let either application-level code or subclasses call
  30. any constructor for the class.  If you want subclasses to have direct
  31. access to the constructors, make them protected instead of private.
  32.  
  33. Arrays are still a problem; if the default constructor for a class X is 
  34. non-public, it's not legal to say "X x_array[size];" or to dynamically 
  35. allocate an array of X with "X *xlist = new X[size];"  It's not that you
  36. get an array whose members have not gone through X::operator new, but
  37. rather that you can't get an array in the first place.
  38.  
  39. Presumably this limitation would go away if X3J16 actually designed an
  40. operator new[] and someone actually implemented it :-)
  41.