home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13577 < prev    next >
Encoding:
Text File  |  1992-09-13  |  2.5 KB  |  54 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: <1992Sep13.160033.7735@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <92254.104002KKEYTE@ESOC.BITNET> <1992Sep10.174914.838@taumet.com>  <92255.112151KKEYTE@ESOC.BITNET> <1992Sep11.174716.3699@taumet.com> <92256.150512KKEYTE@ESOC.BITNET>
  8. Date: Sun, 13 Sep 1992 16:00:33 GMT
  9. Lines: 43
  10.  
  11. Karl Keyte <KKEYTE@ESOC.BITNET> writes:
  12.  
  13. >...  In overloading, for
  14. >example, the '+' operator for one of my own classes, I would try to provide
  15. >functionality which fits with the usual understanding of the operator.  In
  16. >providing '+' for "strings" I would hope to offer concatenation rather than
  17. >truncation with the '+' operator.  That works quite well.  If I require
  18. >something which is unrelated to any operator I would rather not overload
  19. >but provide a member function...
  20.  
  21. This is a very commendable programming style, and I recommend it to
  22. everyone.  It isn't a language rule, however, and probably would not
  23. be enforceable by a compiler if it were.
  24.  
  25. >To do what I have just described with the comma-operator in C++ is not
  26. >possible. [ in the sense of preserving order of evaluation ]
  27.  
  28. Just what we have been saying.  In consequence, I suggest that you
  29. do not overload the comma, but use a non-operator function instead.
  30.  
  31. If a major concern is preserving order of evaluation, then continue
  32. to use the comma just that way and don't overload it.  If you want
  33. to use the comma as a shorthand for a function call, then accept the
  34. function-call semantics.
  35.  
  36. It would be possible to change the rules so that order of evaluation
  37. was preserved for overloaded versions of operators "&&", "||", and ",".
  38. Any proposal to do so would have to show some good reason why the
  39. change is needed.  Personally, I've never seen any example of code
  40. which was improved by using an overloaded comma (which of course
  41. doesn't mean there is no such code).
  42.  
  43. In the case of "&&" and "||", another solution might be to provide
  44. operator void*() for the classes instead, and not overload the logical
  45. functions.  If that doesn't solve the problem, then it seems to me
  46. that the overloaded logical operators are violating the principle of
  47. acting like their built-in versions.  For example, "A && B" doesn't
  48. do anything to A or B, but treats each as a zero/non-zero value.  You
  49. don't need to overload "&&" to get that effect.
  50. -- 
  51.  
  52. Steve Clamage, TauMetric Corp, steve@taumet.com
  53. Vice Chair, ANSI C++ Committee, X3J16
  54.