home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13486 < prev    next >
Encoding:
Text File  |  1992-09-10  |  1.4 KB  |  48 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Answers on a postcard...
  5. Message-ID: <1992Sep10.174914.838@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <92254.104002KKEYTE@ESOC.BITNET>
  8. Date: Thu, 10 Sep 1992 17:49:14 GMT
  9. Lines: 37
  10.  
  11. Karl Keyte <KKEYTE@ESOC.BITNET> writes:
  12.  
  13. |#include <iostream.h>
  14.  
  15. |class X {
  16. |   private:    int           value;
  17. |   public:     int           operator , (int i) { return i+10; };
  18. |               int           operator + (int i) { return value+i; };
  19. |               X &           operator = (int i) { value=i; return *this; };
  20. |};
  21.  
  22.  
  23. |void main()
  24. |{
  25. |   X         x;
  26.  
  27. |   cout << "Value is " << (x=1, x+1) << endl;
  28. |}
  29.  
  30. In the expression in parens, it is unspecified whether "x=1" or
  31. "x+1" is evaluated first.  They are arguments to the "operator,()"
  32. function, not the left and right side of a comma-expression.  If
  33. "x+1" is evaluated first, which is legal, it will use an
  34. uninitialized "x.value".
  35.  
  36. Don't write code which depends on unspecified order of evaluation.
  37.  
  38. If there were no "op,()", the "," would mark a comma-expression,
  39. and the "x=1" would have to be evaluated first; in that case the
  40. whole expression would be well-defined.
  41.  
  42. Finally, use only an "int" return type for main(). The results of
  43. doing otherwise are also undefined.
  44. -- 
  45.  
  46. Steve Clamage, TauMetric Corp, steve@taumet.com
  47. Vice Chair, ANSI C++ Committee, X3J16
  48.