home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / cplus / 1140 < prev    next >
Encoding:
Internet Message Format  |  1992-09-09  |  2.7 KB

  1. From: pabloh@hpwala.wal.hp.com (Pablo Halpern )
  2. Date: Thu, 3 Sep 1992 14:01:16 GMT
  3. Subject: Re: Destruction of Temporaries, Yet Another Posting
  4. Message-ID: <3625@hpwala.wal.hp.com>
  5. Organization: Hewlett-Packard Company
  6. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!sdd.hp.com!scd.hp.com!hplextra!hpcc05!hpcuhb!hpda!hpwala!pabloh
  7. Newsgroups: comp.std.c++
  8. References: <1992Sep2.143007.5655@bnrmtl.bnr.ca> <1992Sep2.171859.852@lucid.com>
  9. Sender: netnews@hpwala.wal.hp.com
  10. Lines: 64
  11.  
  12. In article <1992Sep2.171859.852@lucid.com>, jss@lucid.com (Jerry Schwarz) writes:
  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. Well, until the language support for abstract const is solidified, I
  40. suggest the following:
  41.  
  42.     class String { public:
  43.        operator char*();             // NON-const
  44.        operator const char*() const; // const
  45.        String( const char* );
  46.        ...
  47.     };
  48.  
  49. A non-const string can be converted to a non-const char*, a const string
  50. can only be converted to a const char*.  If only conversion to a const
  51. char* is desired, then I agree with Jerry that the non-const version should
  52. be eliminated.
  53.  
  54. |> I believe the use of the implicitly invoked conversion operator
  55. |> obscures the issue of lifetime of temporaries and that we should 
  56. |> be writing the above example out in full as.
  57. |> 
  58. |>     const char* p = (h+w).pchar();
  59.  
  60. I tend to agree, although I am open to opposing views.
  61.  
  62. |> 
  63. |>   -- Jerry Schwarz
  64.  
  65. -- 
  66.  
  67. - Pablo
  68.  
  69. ------------------------------------------------------------------------
  70. Pablo Halpern             (617) 290-3542
  71. HP Waltham                pabloh@hpwarq.wal.hp.com
  72.  
  73. I am self-employed, so my opinions *do* reflect those of my employer.
  74. However, they may not reflect the opinions of my client.
  75. ------------------------------------------------------------------------
  76.