home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Answers on a postcard...
- Message-ID: <1992Sep10.174914.838@taumet.com>
- Organization: TauMetric Corporation
- References: <92254.104002KKEYTE@ESOC.BITNET>
- Date: Thu, 10 Sep 1992 17:49:14 GMT
- Lines: 37
-
- Karl Keyte <KKEYTE@ESOC.BITNET> writes:
-
- |#include <iostream.h>
-
- |class X {
- | private: int value;
- | public: int operator , (int i) { return i+10; };
- | int operator + (int i) { return value+i; };
- | X & operator = (int i) { value=i; return *this; };
- |};
-
-
- |void main()
- |{
- | X x;
-
- | cout << "Value is " << (x=1, x+1) << endl;
- |}
-
- In the expression in parens, it is unspecified whether "x=1" or
- "x+1" is evaluated first. They are arguments to the "operator,()"
- function, not the left and right side of a comma-expression. If
- "x+1" is evaluated first, which is legal, it will use an
- uninitialized "x.value".
-
- Don't write code which depends on unspecified order of evaluation.
-
- If there were no "op,()", the "," would mark a comma-expression,
- and the "x=1" would have to be evaluated first; in that case the
- whole expression would be well-defined.
-
- Finally, use only an "int" return type for main(). The results of
- doing otherwise are also undefined.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-