Get object address when creating it in for loop

hardcoremore via Digitalmars-d digitalmars-d at puremagic.com
Mon May 5 09:15:42 PDT 2014


How to get and address of newly created object and put it in 
pointer array?


int maxNeurons = 100;
Neuron*[] neurons = new Neuron*[](maxNeurons);

Neuron n;

for(int i = 0; i < maxNeurons; i++)
{
         n = new Neuron();
	neurons[] = &n; // here &n always returns same adress
}

writefln("Thread func complete. Len: %s", neurons);


This script above will print array with all the same address 
values, why is that?


Thanks


More information about the Digitalmars-d mailing list