home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13058 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  3.7 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: <64915@cup.portal.com>
  6. Date: Sat, 29 Aug 92 12:07:38 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.   <64878@cup.portal.com> <23587@alice.att.com>
  12. Lines: 80
  13.  
  14. Andrew Koenig replies:
  15.  
  16. |All right, let's look at the original example again: 
  17. |
  18. |        class String {
  19. |         public:
  20. |                // ...
  21. |                operator const char*();
  22. |                // ...
  23. |        };
  24. |
  25. |        String operator+(const String&, const String&);
  26. |
  27. |This class has a user-defined conversion operator to const char*,
  28. |but the definition of that conversion is compiled separately.  Without
  29. |inspecting it, there is no way to tell whether there is any relationship
  30. |between the result of that conversion and the object being converted.
  31. |That result might a pointer to the object's internal representation,
  32. |or it might be a pointer to a copy that was carefully stored somewhere
  33. |else.
  34.  
  35.  This is quite true. In addition, I don't think it should matter
  36. what the conversion method returns. Since, as you point out below,
  37. it is invoked after the storage class of operator+(a,b) should have
  38. already been determined in this instance.
  39.  
  40. |Now we find the following:
  41. |
  42. |String a = /* some value */;
  43. |String b = /* some other value */;
  44. |
  45. |const char* p = a+b;
  46. |
  47. |Now, the expression a+b is just a shorthand for operator+(a,b), so
  48. |the last declaration above is equivalent to
  49. |
  50. |const char* p = operator+(a,b).operator const char*();
  51. |
  52. |In other words, we call a function, call a member function of the
  53. |result, and assign the result of the member function to p.
  54.  ^^^^^^
  55.  The "result" is the temporary value in question. What is the
  56. storage class of the return *value* of operator+(a,b)? Does it
  57. construct a new object of type String? We don't know. If it
  58. does not, then there's not a temporary in your example anyways.
  59.  
  60.  If operator+(a,b) does, in fact, construct a temporary of type
  61. String, then the compiler could determine its storage class,
  62. perhaps based upon the storage class of the context to which
  63. it is applied. In this case it is not an assignment to 'p',
  64. directly. First the operator const char*(), of our new,
  65. potential temporary, must be evaluated. The result of that
  66. evaluation is assigned to 'p'.
  67.  
  68. |On what basis do you claim that the lifetime of p should control the
  69. |lifetime of operator+(a,b)?  Remember, without reading the definitions
  70. |of operator+ and String::operator const char*, you cannot tell whether
  71. |or not there is actually any connection.  Is this any different from
  72. |the identity function in my earlier example?
  73. |      
  74. |--Andrew Koenig
  75. |                          ark@europa.att.com
  76.  
  77.  So what have we got? Well we've made an assignment to 'p'. We
  78. expect that assignment to remain valid for the life of 'p'. It
  79. would seem that both 'p' and this unnamed "temporary" object
  80. of class String could require the same storage class.
  81. Furthermore, it might be correct to destruct both of them at
  82. the same point. Of course, all kinds of nasty things must be
  83. cleared up whenever 'p' is assigned to other pointers, etc,
  84. which may propogate the lifetime of the unnamed "temporary".
  85.  
  86.  I see that, in a way, we've done something to undermine
  87. the integrity of the String object by allowing it to
  88. represent itself as a const char*. Should we now overload the
  89. global const char* operator= to cooperate with this conversion?
  90. If so, then what scheme shall be used? Reference counting?
  91. Smart Pointers, etc, etc, ...?
  92.  
  93.             Regards, Aurelius@cup.portal.com
  94.