home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11813 < prev    next >
Encoding:
Internet Message Format  |  1992-07-30  |  2.0 KB

  1. Path: sparky!uunet!olivea!bu.edu!news.bbn.com!mips2!granite.ma30.bull.com!horvath
  2. From: horvath@granite.ma30.bull.com (John Horvath)
  3. Newsgroups: comp.lang.c++
  4. Subject: Design of Constructors
  5. Message-ID: <1992Jul30.211945.6463@mips2.ma30.bull.com>
  6. Date: 30 Jul 92 21:19:45 GMT
  7. Sender: news@mips2.ma30.bull.com (Usenet News Manager)
  8. Organization: Bull HN Information Systems Inc.
  9. Lines: 34
  10. Originator: horvath@granite.ma30.bull.com
  11.  
  12. Executive summary: If constructors can't return values, is it their intent
  13. they should not try to do anything that can fail?
  14. -----
  15.  
  16.   Constructors are suppose to be the place were a class's
  17. initialization takes place. Since they can't return a value (directly),
  18. it seems that the only initialization that they should be doing is
  19. stuff that can't fail. (e.g. they shouldn't do things like memory
  20. allocation, open files or access devices). In the FAQ for this group,
  21. it mentions that one possibility for solving a failed constructor is to
  22. include a status data member that indicates if the class is "alive" or
  23. not. Later, other class functions can check if the class is active
  24. before doing any action.
  25.   This seems a little contradictory (shifting initialization checking
  26. to functions who should only be doing their functional purpose). I'm
  27. trying to resolve, for myself, the intended purpose of this limitation
  28. to constructors. If the constructor involves actions that could fail,
  29. should they instead be included in a separate function that can
  30. initialize the class? Something like an activate function that can
  31. return a failed status? But this requires that the class usage has an
  32. explicit protocol which doesn't seem to fit in with my perception of
  33. the "information hiding" paradigm.
  34.  
  35.        constructor (simple initialization)
  36.        activator (completes initialization with error return)
  37.        action_function (can assume activator successful)
  38.  
  39.   I understand how throw & catch can solve this, but I think that using
  40. them still hides the basic question about why constructors were
  41. designed with this restraint.
  42.  
  43.  
  44. john horvath
  45. internet: j.horvath@ma30.bull.com
  46.