source: src/examples/constructors.c @ 15db1ab

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 15db1ab was 097e2b0, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

constructor/destructor, more example programs

  • Property mode set to 100644
File size: 2.7 KB
Line 
1int fred() {
2    // initialize basic structure
3    struct S {
4        int i, j, k;
5    };
6    void ?{}( S *s ) { s->i = 1, s->k = 2; }            // default constructor
7    void ?{}( S *s, int i, int k ) { s->i = i, s->k = k; } // 2 parameter constructor
8    void ?{}( S *s, S c ) { *s = c; }                   // copy constructor
9    void ^?{}( S *s ) { s->i = 0, s->k = 0; }           // default destructor
10    void ^?{}( S *s, int i ) { s->i = i, s->k = i; }    // 1 parameter destructor
11    {
12        S s1;                   // default constructor
13        S s2 = { 3, 7 };        // 2 parameter constructor
14        S s3 @= { .k:3, .i:7 }; // 2 parameter C initialization
15        ?{}( &s3, 2, 5 );       // explicit 2 parameter constructor
16        ^?{}( &s1 );            // explicit call to default destructor
17    } // implicit call to default destructor for s2, explicit call s1, no call for s3
18    S s4 @= {};                 // no default construction
19    (&s4){ 2, 5 };              // explicit 2 parameter constructor
20    ^s4{ 3 };                   // explicit call to 1 parameter destructor
21
22    // initialize pointer to a basic structure
23
24    void ?{}( S **s ) { *s = malloc(); (*s)->i = 1, (*s)->k = 2; } // default constructor
25    void ?{}( S **s, int i, int k ) { *s = malloc(); (*s)->i = i, (*s)->k = k; } // 2 parameter constructor
26    void ^?{}( S **s ) { (*s)->i = 0, (*s)->k = 0; free( *s ); *s = 0; } // default destructor
27    {
28        S *ps1;                 // default constructor
29        S *ps2 = { 3, 7 };      // 2 parameter constructor
30        S *ps3 @= 0;            // C initialization
31        S *ps4 @= {};           // no default construction
32    } // implicit call to default destructor for ps2 and ps1, checks ordering of explicit destructor calls
33
34    ?{}( &ps3, 2, 5 );          // explicit 2 parameter constructor
35    (&ps4){ 2, 5 };             // explicit 2 parameter constructor
36   
37    ^?{}( &ps3 );               // explicit call to default destructor
38    ^ps4{};                     // explicit call to default destructor
39
40    // initialize complex structure
41
42    struct T {
43        struct S s;
44    };
45
46    void ?{}( T *t ) {}                                 // default constructor => implicitly call constructor for field s
47    void ?{}( T *t, int i, int k ) { (&t->s){ i, k }; } // 2 parameter constructor => explicitly call constructor for field s
48    void ?{}( T *t, S c ) { (&t->s){ c }; }             // 1 parameter constructor => explicitly call copy constructor for field s
49    void ^?{}( T *s, int i ) {}                         // destructor => implicitly call destructor for field s
50    {
51        S s;                    // default constructor
52        T t1;                   // default constructor
53        T t2 = { s };           // 1 parameter constructor
54        ^?{}( &t1 );            // explicit call to default destructor => implicit call to t1.s's destructor
55    } // implicit call to default destructor for t2 and implicit call for s;
56    T t3;                       // default constructor
57    T t4 @= { { 1, 3 } };       // C initialization
58    (&t4){ 2, 5 };              // explicit 2 parameter constructor
59
60    T *pt = malloc(){ 3, 4 };   // common usage
61} // implicit call to default destructor for t3
Note: See TracBrowser for help on using the repository browser.