home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / std / cplus / 1122 < prev    next >
Encoding:
Text File  |  1992-09-02  |  1.8 KB  |  53 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!stanford.edu!lucid.com!lucid.com!jss
  3. From: jss@lucid.com (Jerry Schwarz)
  4. Subject: Re: Destruction of Temporaries, Yet Another Posting
  5. Message-ID: <1992Sep2.171859.852@lucid.com>
  6. Sender: usenet@lucid.com
  7. Reply-To: jss@lucid.com (Jerry Schwarz)
  8. Organization: Lucid, Inc.
  9. References:  <1992Sep2.143007.5655@bnrmtl.bnr.ca>
  10. Date: Wed, 2 Sep 92 17:18:59 GMT
  11. Lines: 40
  12.  
  13. In article <1992Sep2.143007.5655@bnrmtl.bnr.ca>, john@bnrmtl.bnr.ca (John Hickin) writes:
  14. |> 
  15. |> A compiler using the const temporary approach and a thoughtful programmer
  16. |> can combine to make code like that of the String example safer:
  17. |> 
  18. |>    class String { public:
  19. |>        operator const char*(); // NON-const
  20. |>        String( const char* );
  21. |>        ...
  22. |>    };
  23. |> 
  24.  
  25. Making the function that gives the pointer to char a non-const is a mistake.
  26. The function may modify the underlying representation, but it certainly
  27. does not modify the represented character sequence.  There is no
  28. reason that I shouldn't be able to apply it to a const String
  29.  
  30. |>    String h( "hello " ), w( "world" );
  31. |>    const char* p = h + w;
  32.  
  33. A careful programmer might have written
  34.  
  35.     const String h("hello "), w(" world") ;
  36.  
  37. and be surprised that  (const char*)h isn't allowed.
  38.  
  39. String with a conversion operator seems to have caught on in this
  40. newsgroup as "the" example for discussing lifetime of temporaries.
  41. It should be noted that because of all the problems associated with
  42. such an operator the x3j16 library working group has decided that
  43. this functionality will be provided by an ordinary member function
  44. and not by a conversion operator.
  45.  
  46. I believe the use of the implicitly invoked conversion operator
  47. obscures the issue of lifetime of temporaries and that we should 
  48. be writing the above example out in full as.
  49.  
  50.     const char* p = (h+w).pchar();
  51.  
  52.   -- Jerry Schwarz
  53.