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

Regan Heath regan at netmail.co.nz
Mon May 28 15:10:24 PDT 2007


Manfred Nowak Wrote:
> Steve Teale wrote
> 
> > how do I get the semantics I'd get with a plain old pointer to the
> > object? 
> 
> You cannot use?:
> Node[]* t = &node.parent.nodelist;

Manfreds rather brief reply is exactly what I was going to suggest.  The nice thing about D is that a struct, class, pointer, reference, pointer to pointer or reference, pointer to pointer to .. you get the idea;  all get de-referenced by the '.' operator, there is no silly '->' operator for pointers.

So, by taking a pointer to the reference you should be able to modify the reference via the pointer by saying:

int n = t.length;
t.length = n+1;

or

t.length = t.length+1;

Regan Heath



More information about the Digitalmars-d mailing list