Reference to an object disregarding mutation - what's the trick?

Mike Parker aldacron71 at yahoo.com
Mon May 28 17:33:11 PDT 2007


Steve Teale wrote:
> 
> For brevity I want to be able to say:
> Node[] t = node.parent.nodelist;
> int n = t.length;
> t.length = n+1;
> .....
> 
> But as soon as I modify t it becomes a separate object.

No, it doesn't become a separate object when you modify it, it does so 
when you declare it. Arrays are objects that consist of a length field 
and a data pointer. Consider this line:

Node[] t = node.parent.nodelist;

What happens here is that t is, internally, a new array object which has 
the same length as and points to the same data as node.parent.nodelist. 
So when you modify t's length, you are modifying the new object and not 
the original.

> OK, I can use with, but how do I get the semantics I'd get with a plain old pointer to the object?

Your choices are to either use with as you mention or, as Manfred 
suggested, use a pointer to the original array object.



More information about the Digitalmars-d mailing list