Is this a desing rationale? (static array object member)

Brian Hsu brianhsu.hsu at gmail.com
Sat Sep 29 03:10:20 PDT 2007


downs Wrote:

> > Finally, is this behavior reasonable? Since I didn't declare that int [] as a static class member, even though they have same initialization array literal, but I would expect that a.z/b.z they should be different array have same content. (As in Java or C++)
> That's exactly what they are.
> Different arrays with the same content.

Sorry for ambiguous, what I mean is different array _instance_ of same value instead of different pointers point to same array instance. Like same code in Java/C++ that a.z and b.z would point to different array instance even they have same initialization, change element at a.z won't have side effect to b.z.

Since I write Java/C++ programing before learning D, it is just a little bit strange for me that object member array acts like class member array when semantic level they should be different array instance.

So, is the following statement true?  

The compiler will create 3 array instance (Test.z, fun1.x, fun2.x) since they appear 3 time, and they will be at last translate to memory address at compile time. So in facts the code is looks like:

class Test
{
    int [] z = 0xFFFFF; // memory address of the array create by array literal
}

That's why different instance of class Test will have object member z point to same array instance. Is this correctly?

If it is, I will add a page at Wiki4D to mention this behavior since it is a little bit strange for people who programming Java/C++ code before.



More information about the Digitalmars-d mailing list