home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13045 < prev    next >
Encoding:
Text File  |  1992-08-29  |  3.6 KB  |  89 lines

  1. Path: sparky!uunet!cs.utexas.edu!rutgers!att!att!allegra!alice!ark
  2. From: ark@alice.att.com (Andrew Koenig)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: complex classes and temporary destruction.
  5. Message-ID: <23587@alice.att.com>
  6. Date: 29 Aug 92 14:31:31 GMT
  7. References: <MCGRANT.92Aug26232410@rascals.stanford.edu> <23563@alice.att.com> <64821@cup.portal.com> <23578@alice.att.com> <64878@cup.portal.com>
  8. Reply-To: ark@alice.UUCP ()
  9. Organization: AT&T Bell Laboratories, Murray Hill NJ
  10. Lines: 77
  11.  
  12. In article <64878@cup.portal.com> Aurelius@cup.portal.com (Mark Christian Barnes) writes:
  13.  
  14. >  But Andrew, you have changed the statement completely. The
  15. > temporary 's+t' is no longer even being assigned to 'p'. It is
  16. > constructed as a parameter for function (or macro) 'I'. In this
  17. > case I would suggest that 's+t' is an auto variable with a scope
  18. > limited to 'I'.
  19.  
  20. No, I haven't changed it at all.  I took a value and passed it to
  21. a function that returns that same value, unchanged.  Why on earth should
  22. doing that affect the behavior of the program?
  23.  
  24. >  I know that this means that your example will not work under
  25. > this interpretation. Well I don't think it should. The perpetual
  26. > problem of aliasing, to which pointers contribute, should not be
  27. > solved by the compiler. I feel that the programmer should pay
  28. > attention to what he/she is doing.
  29.  
  30. If programmers always paid attention to what they were doing,
  31. any solution would work.  Indeed, there would be no need for
  32. type checking -- just pay attention all the time and you won't
  33. make any type errors.  Ditto for any other kind of checking,
  34. for that matter.
  35.  
  36. Seriously, now, a large part of the discussion of lifetime of
  37. temporaries is an attempt to avoid surprising people who don't
  38. always pay attention to what they do.
  39.  
  40. >  In your example, an expression is evaluated and passed
  41. > (on the stack) as the parameter to a function. The function
  42. > 'I' returns another value of type pointer to char. There should
  43. > not be any assumption that the parameter and the return value
  44. > have anything in common. You, as the programmer, know that 'I'
  45. > returns the parameter. Therefore, you should also insure that
  46. > any parameters passed to 'I' are of the appropriate storage
  47. > class and scope for the uses that you intend.
  48.  
  49. All right, let's look at the original example again:
  50.  
  51.     class String {
  52.     public:
  53.         // ...
  54.         operator const char*();
  55.         // ...
  56.     };
  57.  
  58.     String operator+(const String&, const String&);
  59.  
  60. This class has a user-defined conversion operator to const char*,
  61. but the definition of that conversion is compiled separately.  Without
  62. inspecting it, there is no way to tell whether there is any relationship
  63. between the result of that conversion and the object being converted.
  64. That result might a pointer to the object's internal representation,
  65. or it might be a pointer to a copy that was carefully stored somewhere
  66. else.  Now we find the following:
  67.  
  68.     String a = /* some value */;
  69.     String b = /* some other value */;
  70.  
  71.     const char* p = a+b;
  72.  
  73. Now, the expression a+b is just a shorthand for operator+(a,b), so
  74. the last declaration above is equivalent to
  75.  
  76.     const char* p = operator+(a,b).operator const char*();
  77.  
  78. In other words, we call a function, call a member function of the
  79. result, and assign the result of the member function to p.
  80.  
  81. On what basis do you claim that the lifetime of p should control the
  82. lifetime of operator+(a,b)?  Remember, without reading the definitions
  83. of operator+ and String::operator const char*, you cannot tell whether
  84. or not there is actually any connection.  Is this any different from
  85. the identity function in my earlier example?
  86. -- 
  87.                 --Andrew Koenig
  88.                   ark@europa.att.com
  89.