Why can't we define re-assignable const reference variable?
Janice Caron
caron800 at googlemail.com
Thu Feb 21 07:45:37 PST 2008
On 21/02/2008, Sergey Gromov <snake.scaly at gmail.com> wrote:
> Janice Caron <caron800 at googlemail.com> wrote:
> This is Ok. It's changing the .length directly which I don't like, and
> which is actually unpredictable.
It's always safe, and it's always predictable. It's just not very useful.
string s = "hello";
s.length = 10;
s will now contain "hello" followed by five default-value chars. Given
that string contents are immutable, that's completely pointless. But
it makes a lot more sense with non-constant arrays. For example:
char[] s;
s.length = 5;
/* blah */
s.length = 25;
/* blah */
s.length = 10;
etc. In this case, the buffer will be reallocated somewhere else, and
the original contents copied so you don't lose anything. It's exactly
like calling realloc() in C or C++.
More information about the Digitalmars-d
mailing list