home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12725 < prev    next >
Encoding:
Text File  |  1992-08-21  |  3.8 KB  |  86 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.714407976@centerline.com>
  6. Sender: news@centerline.com
  7. Nntp-Posting-Host: rapier
  8. Organization: CenterLine Software, Inc.
  9. References: <1992Aug19.231926.28218@sunb10.cs.uiuc.edu> <2273@devnull.mpd.tandem.com> <matt.714318343@centerline.com> <2284@devnull.mpd.tandem.com>
  10. Distribution: usa
  11. Date: Fri, 21 Aug 1992 14:39:36 GMT
  12. Lines: 72
  13.  
  14. In <2284@devnull.mpd.tandem.com> rgp@mpd.tandem.com (Ramon Pantin) writes:
  15. >So, to get an error code out of a constructor we should (note that I
  16. >DON'T agree with all this!):
  17. >
  18. >- Forbid the creation of automatic variables and arrays, i.e. make
  19. >  the constructors non-public.
  20. >
  21. >- Do the new/constructor thing.
  22. >
  23. >- Have some public static member function that creates objects.
  24. >
  25. >Sounds pretty convuluted, don't you think?
  26. >No wonder nobody seems to write simple code anymore!
  27.  
  28. Well, as the person who pointed out how one can make constructors private,
  29. let me hasten to add that I don't actually recommend this approach; I was
  30. just pointing out that it *is* possible.
  31.  
  32. In addition to the "forbid automatic objects and overload operator new"
  33. approach, there are two other things one can do about constructor failures
  34. in a C++ environment that lacks exceptions:
  35.  
  36. (1)  Have an "isValid" bit in every object, which the constructor sets
  37.      or clears before returning.  Application code is then responsible 
  38.      for testing "obj.isValid" before using any object.  I know of a 
  39.      couple of different class libraries that have taken this approach.
  40.      The penalty for not testing the isValid bit is using a potentially 
  41.      uninitialized or invalid object.
  42.  
  43. (2)  Try very hard to write constructors that cannot fail, and move any
  44.      failure-prone initialization (like dynamic allocation) to some 
  45.      secondary initialization routine.  Application code then becomes 
  46.      responsible for calling "obj.Init()" before using any object.  The 
  47.      penalty for omitting the call is using a potentially uninitialized 
  48.      or invalid object.
  49.  
  50.      You can sort of get around this problem if you can make sure the
  51.      Init member function requires no arguments (so it can be called 
  52.      from anywhere within the class's own implementation), keep an 
  53.      isInitialized bit in the object, have EVERY member function of 
  54.      the object test isInitialized and call Init if needed, and forbid 
  55.      the use of public data members, escshewing them in favor of inline
  56.      member functions of the form:
  57.  
  58.     inline int X::Count(void) const
  59.     {
  60.         if (!isInitialized) Init();
  61.         return _count;
  62.     }
  63.  
  64.      Of course, this approach is also a big kludge, inconvenient to 
  65.      maintain, and subject to code size explosion if you have a large
  66.      number of short member functions.
  67.  
  68. The fact that both of these approaches leave you with the potential for
  69. using an incompletely initialized object (the very thing constructors in
  70. C++ were supposed to prevent!) points to a basic shortcoming in most of
  71. the current implementations of the language.  
  72.  
  73. The underlying point is that without a decent exception system, there 
  74. is **NO** good way to deal with the problem of constructor failures.  
  75. There are several different not-so-good ways to choose from; which one 
  76. you choose depends on the nature of the system you're writing.
  77.  
  78. Even *with* a decent exception system, the problem doesn't just go away.
  79. Exceptions provide a mechanism and a framework within which one can deal
  80. with this problem (as well as many other problems).  The design skills
  81. and engineering discipline needed to use exceptions both correctly and 
  82. robustly are still not to be underestimated.
  83. --
  84.  Matt Landau            Waiting for a flash of enlightenment
  85.  matt@centerline.com              in all this blood and thunder
  86.