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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!mips!darwin.sura.net!europa.asd.contel.com!news.ans.net!newsgate.watson.ibm.com!yktnews!admin!!mittle
  3. From: mittle@watson.ibm.com (Josh Mittleman)
  4. Subject: Re: Return value for the constructors.
  5. Sender: news@watson.ibm.com (NNTP News Poster)
  6. Message-ID: <1992Aug19.173619.40943@watson.ibm.com>
  7. Date: Wed, 19 Aug 1992 17:36:19 GMT
  8. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  9. References: <3462@unisql.UUCP> <1992Aug18.210209.15023@watson.ibm.com> <3468@unisql.UUCP>
  10. Nntp-Posting-Host: siena.watson.ibm.com
  11. Organization: IBM T.J. Watson Research Center
  12. Lines: 30
  13.  
  14. Discussing why it would be useful for a constructor to have a return value,
  15. Nandraj Arni wrote:
  16.  
  17. >   The reason I think it is a important feature the constructor should
  18. > return whether it was a success or not?
  19.  
  20. This is a real concern, but a return value is not the right solution.  As
  21. has been noted, the best solution is the still-pending exception handling
  22. system: The constructor throws an exception which is caught by the
  23. surrounding code.  Until that system is in place in the language, there are
  24. several ways to model it.  One is to include a status flag in the object.
  25. In some cases it is appropriate for this to be a member of the object, in
  26. others it would be better as a static class member.  In either case, you
  27. can test the status flag after construction.
  28.  
  29. Note that actually having the constructor return its status value would
  30. mess up the language pretty badly.  Consider:
  31.  
  32.     class Thing;
  33.  
  34.     cout << Thing(7, "a", 1.5);
  35.  
  36. This second statement is correct and meaningful, since the right-hand-side
  37. is a Thing.  If the constructor returned an int, then it would be
  38. impossible to use a temporary class object without declaring a variable for
  39. it. 
  40.  
  41. ===========================================================================
  42. Josh Mittleman (mittle@watson.ibm.com)
  43. J2-C28 T.J. Watson Research Center, PO Box 704, Yorktown Heights, NY  10598
  44.