home *** CD-ROM | disk | FTP | other *** search
- From: pabloh@hpwala.wal.hp.com (Pablo Halpern )
- Date: Thu, 3 Sep 1992 14:01:16 GMT
- Subject: Re: Destruction of Temporaries, Yet Another Posting
- Message-ID: <3625@hpwala.wal.hp.com>
- Organization: Hewlett-Packard Company
- 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
- Newsgroups: comp.std.c++
- References: <1992Sep2.143007.5655@bnrmtl.bnr.ca> <1992Sep2.171859.852@lucid.com>
- Sender: netnews@hpwala.wal.hp.com
- Lines: 64
-
- In article <1992Sep2.171859.852@lucid.com>, jss@lucid.com (Jerry Schwarz) writes:
- |> In article <1992Sep2.143007.5655@bnrmtl.bnr.ca>, john@bnrmtl.bnr.ca (John Hickin) writes:
- |> |>
- |> |> A compiler using the const temporary approach and a thoughtful programmer
- |> |> can combine to make code like that of the String example safer:
- |> |>
- |> |> class String { public:
- |> |> operator const char*(); // NON-const
- |> |> String( const char* );
- |> |> ...
- |> |> };
- |> |>
- |>
- |> Making the function that gives the pointer to char a non-const is a mistake.
- |> The function may modify the underlying representation, but it certainly
- |> does not modify the represented character sequence. There is no
- |> reason that I shouldn't be able to apply it to a const String
- |>
- |> |> String h( "hello " ), w( "world" );
- |> |> const char* p = h + w;
- |>
- |> A careful programmer might have written
- |>
- |> const String h("hello "), w(" world") ;
- |>
- |> and be surprised that (const char*)h isn't allowed.
-
- Well, until the language support for abstract const is solidified, I
- suggest the following:
-
- class String { public:
- operator char*(); // NON-const
- operator const char*() const; // const
- String( const char* );
- ...
- };
-
- A non-const string can be converted to a non-const char*, a const string
- can only be converted to a const char*. If only conversion to a const
- char* is desired, then I agree with Jerry that the non-const version should
- be eliminated.
-
- |> I believe the use of the implicitly invoked conversion operator
- |> obscures the issue of lifetime of temporaries and that we should
- |> be writing the above example out in full as.
- |>
- |> const char* p = (h+w).pchar();
-
- I tend to agree, although I am open to opposing views.
-
- |>
- |> -- Jerry Schwarz
-
- --
-
- - Pablo
-
- ------------------------------------------------------------------------
- Pablo Halpern (617) 290-3542
- HP Waltham pabloh@hpwarq.wal.hp.com
-
- I am self-employed, so my opinions *do* reflect those of my employer.
- However, they may not reflect the opinions of my client.
- ------------------------------------------------------------------------
-