home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13039 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  2.3 KB

  1. Path: sparky!uunet!portal!cup.portal.com!Aurelius
  2. From: Aurelius@cup.portal.com (Mark Christian Barnes)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: complex classes and temporary destruction.
  5. Message-ID: <64878@cup.portal.com>
  6. Date: Fri, 28 Aug 92 21:50:51 PDT
  7. Organization: The Portal System (TM)
  8. Distribution: world
  9. References: <MCGRANT.92Aug26232410@rascals.stanford.edu>
  10.   <23563@alice.att.com> <64821@cup.portal.com> <23578@alice.att.com>
  11. Lines: 49
  12.  
  13. Andrew Koenig writes:
  14.  
  15. |>  Have the temporary "inherit" (for lack of a better word) the
  16. |> storage class of p. If p is an auto variable then 's+t' should
  17. |> be auto too. Destroy p and 's+t' together. If p is static, then
  18. |> perhaps have 's+t' be static as well, onc instance of 's+t'
  19. |> gets re-used each time the statement is executed.
  20. |
  21. |I'm afraid that wouldn't work.  Consider this:
  22. |
  23. |char* I(char* x) { return x; }
  24. |
  25. |The function I (for identity) just returns its argument, but to make things
  26. |difficult, it is compiled separately.  Now, you would like
  27. |
  28. |char* p = s+t;
  29. |
  30. |to cause the temporary for s+t to stick around as long as p does,
  31. |but what are you going to do about this?
  32. |
  33. |        char* p = I(s+t);
  34. |
  35. |There's no way to know what I() does, so what basis do you have for
  36. |letting the lifetime of p affect that of s+t?
  37.  
  38.  But Andrew, you have changed the statement completely. The
  39. temporary 's+t' is no longer even being assigned to 'p'. It is
  40. constructed as a parameter for function (or macro) 'I'. In this
  41. case I would suggest that 's+t' is an auto variable with a scope
  42. limited to 'I'.
  43.  
  44.  I know that this means that your example will not work under
  45. this interpretation. Well I don't think it should. The perpetual
  46. problem of aliasing, to which pointers contribute, should not be
  47. solved by the compiler. I feel that the programmer should pay
  48. attention to what he/she is doing.
  49.  
  50.  In your example, an expression is evaluated and passed
  51. (on the stack) as the parameter to a function. The function
  52. 'I' returns another value of type pointer to char. There should
  53. not be any assumption that the parameter and the return value
  54. have anything in common. You, as the programmer, know that 'I'
  55. returns the parameter. Therefore, you should also insure that
  56. any parameters passed to 'I' are of the appropriate storage
  57. class and scope for the uses that you intend.
  58.  
  59. <whew, that's enough for now.>
  60.  
  61.             Regards, Aurelius@cup.portal.com
  62.