array setting with objects (bug?)

Denton Cockburn diboss at hotmail.com
Tue Mar 4 06:00:18 PST 2008


On Tue, 04 Mar 2008 08:53:35 -0500, Jarrett Billingsley wrote:

> "Denton Cockburn" <diboss at hotmail.com> wrote in message 
> news:pan.2008.03.04.13.32.00.785638 at hotmail.com...
> 
>> arr[] = new C; /* this is the offending line, it doesn't happen if I */
> 
> This creates *one* instance of C and sets all the elements of arr to point 
> to that single instance.
> 
> Hence:
> 
>> [5]
>> [5,5]
>> [5,5,5]
> 
> You keep modifying the same array.  :)

Yeah, would be nice if that was mentioned somewhere.

in the section of the docs where:
s[] = 3;		// same as s[0] = 3, s[1] = 3, s[2] = 3

I (naively) expected:

s[] = new C;  // to be same as s[0] = new C, s[1] = new C, s[2] = new C
or
s[] = foo(); // where foo returns an object (a different one each time)

easy to work with and around as is, but would be rather nice to be able to
do what I just stated.  Syntactic sugar for 2.0?



More information about the Digitalmars-d mailing list