source: tests/raii/memberCtors.cfa

Last change on this file was 6c7fe7b, checked in by Peter A. Buhr <pabuhr@…>, 10 months ago

change from printf to sout

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[6c7fe7b]1#include <fstream.hfa>
2
[4d4882a]3struct WrappedInt {
[6c7fe7b]4        int x;
5        int id;
[4d4882a]6};
[a42a654]7int intID = 0;
[4d4882a]8
[6c7fe7b]9void ?{}( WrappedInt & this ) {
10        this.id = intID++;
11        sout | "constructing int id: " | this.id;
12        this.x = 0;
[4d4882a]13}
14
[6c7fe7b]15void ?{}( WrappedInt & this, WrappedInt other ) {
16        this.id = intID++;
17        sout | "copy constructing int: " | other.x | "id: " | this.id;
18        this.x = other.x;
[4d4882a]19}
20
[6c7fe7b]21void ?{}( WrappedInt & this, int x ) {
22        this.id = intID++;
23        sout | "constructing int: " | x | "id: " | this.id;
24        this.x = x;
[4d4882a]25}
26
[6c7fe7b]27void ^?{}( WrappedInt & this ) {
28        sout | "destructing int: " | this.x | "id: " | this.id;
[4d4882a]29}
30
[6c7fe7b]31/* WrappedInt */ void ?=?( WrappedInt & this, int x ) {
32        sout | "assigning int: " | this.x | x | "id: " | this.id;
33        this.x = x;
34        // return this;
[4d4882a]35}
36
[6c7fe7b]37// WrappedInt ?=?( WrappedInt & this, WrappedInt other ) {
38//   sout | "assigning int: " | this.x | other.x;
[72f85de]39//   this.x = other.x;
40//   return this;
41// }
42
[4d4882a]43struct A {
[6c7fe7b]44        WrappedInt x, y, z;
45        int id;
[4d4882a]46};
[a42a654]47int AID = 0;
[4d4882a]48
[6c7fe7b]49void ?{}( A & a ) {
50        // currently must define default ctor, since there's no "= default" syntax
51        a.id = AID++;
52        sout | "default construct A" | a.id;
[235114f]53}
54
[6c7fe7b]55void ?{}( A & a, int x ) {
56        a.id = AID++;
57        sout | "begin construct A id: " | a.id;
58        sout | "construct a.x";
59        (a.x){ x+999 };
60        sout | "assign a.y";
61        a.y = 0; // not a constructor - default constructor will be inserted
62        sout | "end construct A";
[4d4882a]63} // z never constructed - will be automatically default constructed
64
[6c7fe7b]65void ?{}( A & this, A other ) {
66        this.id = AID++;
67        sout | "begin copy construct A id: " | this.id;
68        sout | "copy construct this.x";
69        (this.x){ other.x };
70        sout | "assign this.y";
71        this.y = other.y; // not a constructor - copy constructor will be inserted
72        sout | "end copy construct A";
[4d4882a]73} // z never constructed - will be automatically copy constructed
74
[6c7fe7b]75A ?=?( A & this, A other ) {
76        sout | "begin ?=? A id: " | this.id;
77        this.x = other.x;
78        this.y = other.y;
79        this.z = other.z;
80        sout | "end ?=? A";
81        return this;
[ddbde34]82}
83
[4d4882a]84struct B {
[6c7fe7b]85        A a1, a2, a3;
86        int id;
[4d4882a]87};
[a42a654]88int BID = 0;
[4d4882a]89
[6c7fe7b]90void ?{}( B & b ) {
91        b.id = BID++;
92        sout | "begin construct B id: " | b.id;
93        sout | "assign b.a2";
94        b.a2 = (A){ 2 };
95        sout | "construct b.a1";
96        (b.a1){ 1 };
[4d4882a]97#ifdef ERR1
[6c7fe7b]98        (b.a2){ b.a3 }; // error, b->a2 was used previously but is explicitly constructed
[4d4882a]99#endif
[6c7fe7b]100        sout | "end construct B";
[4d4882a]101} // a2, a3 never constructed - will be automatically default constructed
102
[6c7fe7b]103void ^?{}( B & b ) {
104        b.id = BID++;
105        sout | "begin destruct B id: " | b.id;
106        b.a2 = (A) { 0 };
107        ^(b.a1){};
108        sout | "end destruct B";
[4d4882a]109} // a2, a3 never destructed - will be automatically destructed
110
111int main() {
[6c7fe7b]112        sout | "Before declaration of b1";
113        B b1;  // b1 = { { 1000, 0, 0 }, { 1001, 0, 0 }, { 0, 0, 0 } }
114        sout | "Before declaration of b2";
115        B b2 = b1;
116        sout | "End of main";
[4d4882a]117}
Note: See TracBrowser for help on using the repository browser.