Debian Codegraph @

src/test/01-Basic/237-ctor-arrays.ck

raw
1 // specifying and overloading class constructors within array decl
2
3 // a class
4 class Foo
5 {
6 // a member variable (this will be initialized before
7 // any actual constructors, as Foo's "pre-constructor")
8 0 => int num;
9
10 // constructor 1 "default"
11 fun void Foo()
12 {
13 1 => num;
14 <<< "constructor 1:", num >>>;
15 }
16
17 // constructor 2
18 fun void Foo( int x )
19 {
20 x => num;
21 <<< "constructor 2:", x >>>;
22 }
23
24 // constructor 3
25 fun void Foo( int x, int y )
26 {
27 x*y => num;
28 <<< "constructor 3:", x, y >>>;
29 }
30 }
31
32 // invoke constructor for each array elements in array decl
33 Foo array1()[2];
34 // print each element's num
35 for( auto f : array1 ) <<< f.num, "" >>>;
36
37
38 // invoke constructor for each array elements in array decl
39 Foo array2(2)[3];
40 // print each element's num
41 for( auto f : array2 ) <<< f.num, "" >>>;
42
43 // instantiate an array of Foo, invoking constructor for each
44 new Foo(6,5)[4] @=> Foo array3[];
45 // print each element's num
46 for( auto f : array3 ) <<< f.num, "" >>>;