how can I get a reference of array?

zhmt via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 4 22:42:57 PST 2015


Here is a simple code snippet:
class A {
	public int[] arr;
}

class B {
	public int[] arr;
}

void main()
{
	int[] arr;

	A a = new A;
	B b = new B;
	a.arr = arr;
	b.arr = arr;


	arr ~= 1;
	arr ~= -2;

	foreach(data; a.arr)
	{
		writeln(data);
	}

	foreach(data; b.arr)
	{
		writeln(data);
	}
}

it prints nothing, I know that a.arr and b.arr are all slices of 
arr.

But if a and b want to reference the global arr, it means that 
any changes in arr will be seen by a and b, and vice versa.

How to?

Thanks ahead.


More information about the Digitalmars-d-learn mailing list