class array

Deewiant deewiant.doesnotlike.spam at gmail.com
Tue Mar 20 11:01:43 PDT 2007


orgoton wrote:
> I created a class and now I want to create an array (at compile time) so I did 
> 
> public myClass myArray[]=new myClass(5);
> 
> It didn't compile because of the parameters, so I changed to
> 
> public myClass myArray[]=new myClass()(5);
> 
> Since myClass ctor doesn't take parameters. It mentioned that was expecting ";" and found "(" so I tried the C++ way
> 
> public myClass myArray[]=new[5] myClass();
> 
> Then again, "[" wasn't expected. How do I then construct 5 objects?

public myClass[] myArray = new myClass[5];

or the equivalent

public myClass[] myArray = new myClass[](5);

Note that the D-style declaration syntax myClass[] myArray is generally
preferred over the C-style myClass myArray[].

Also note that this generates just the array: each class reference within the
array will be null until initialized, for instance like so:

foreach (inout c; myArray)
	c = new myClass();


More information about the Digitalmars-d-learn mailing list