Thread overview |
---|
March 20, 2007 class array | ||||
---|---|---|---|---|
| ||||
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? |
March 20, 2007 Re: class array | ||||
---|---|---|---|---|
| ||||
Posted in reply to orgoton | 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();
|
Copyright © 1999-2021 by the D Language Foundation