home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- 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
- From: mittle@watson.ibm.com (Josh Mittleman)
- Subject: Re: Return value for the constructors.
- Sender: news@watson.ibm.com (NNTP News Poster)
- Message-ID: <1992Aug19.173619.40943@watson.ibm.com>
- Date: Wed, 19 Aug 1992 17:36:19 GMT
- Disclaimer: This posting represents the poster's views, not necessarily those of IBM
- References: <3462@unisql.UUCP> <1992Aug18.210209.15023@watson.ibm.com> <3468@unisql.UUCP>
- Nntp-Posting-Host: siena.watson.ibm.com
- Organization: IBM T.J. Watson Research Center
- Lines: 30
-
- Discussing why it would be useful for a constructor to have a return value,
- Nandraj Arni wrote:
-
- > The reason I think it is a important feature the constructor should
- > return whether it was a success or not?
-
- This is a real concern, but a return value is not the right solution. As
- has been noted, the best solution is the still-pending exception handling
- system: The constructor throws an exception which is caught by the
- surrounding code. Until that system is in place in the language, there are
- several ways to model it. One is to include a status flag in the object.
- In some cases it is appropriate for this to be a member of the object, in
- others it would be better as a static class member. In either case, you
- can test the status flag after construction.
-
- Note that actually having the constructor return its status value would
- mess up the language pretty badly. Consider:
-
- class Thing;
-
- cout << Thing(7, "a", 1.5);
-
- This second statement is correct and meaningful, since the right-hand-side
- is a Thing. If the constructor returned an int, then it would be
- impossible to use a temporary class object without declaring a variable for
- it.
-
- ===========================================================================
- Josh Mittleman (mittle@watson.ibm.com)
- J2-C28 T.J. Watson Research Center, PO Box 704, Yorktown Heights, NY 10598
-