Separate dynamic object arrays for multiple instances of overlying object

Jonathan daemon512 at gmail.com
Thu Mar 14 09:43:09 UTC 2024


I'm having some trouble creating arrays that are themselves 
unique within multiple instances of an object.  Example code:


```
class individual
{
     int a = 1; // placeholder for data
}

class individualList
{
     individual[] individuals;  // Something likely has to change 
here
}

void main()
{
     individualList list1 = new  individualList();
     assert(list1.individuals.length == 0);
     individual person1 = new individual();
     list1.individuals ~= person1;
     assert(list1.individuals.length == 1);

     individualList list2 = new individualList();
     assert(list2.individuals.length == 0); // breaks, 
list2.individuals.length is 1
}
```


I'd like the array "individuals" to be unique for each instance 
of individualList, but it seems to be using the same list among 
the different instances.  The following code doesn't  work as I 
don't know the required length of the arrays:
```
class individualList
{
     individual[] individuals = new individual[];  // Doesn't 
compile, requires length
}
```
I've looked a  bit and don't see a solution to this within the 
documentation for arrays or objects.

Further information:
This is being used to parse a xml-style file, the example code is 
a base example, I'm having to do this list population a few 
times, one within another.  Some lists have their count displayed 
before starting to add "individuals" so I could use a 
constructor, but some do not (to my  knowledge) have that  
option, and I'd rather use one solution through the whole parser.


More information about the Digitalmars-d mailing list