3 # Program to demonstrate constructors and destructors. In particular,
4 # test constructors and destructors with stack-instantiated objects.
6 # There are three special assignment situations where constructors may run:
7 # a direct declaration on the stack, in a class definition, and as
8 # a type default. And, of course, constructors run without any assignment
18 public global constructor method void blah99()
20 stdio.stdout->show("global constructor called (fubar)");
24 constructor method void blah1()
26 stdio.stdout->show("constructor called", this.x);
30 destructor method void blah1()
32 stdio.stdout->show("destructor called");
33 stdio.stdout->show("result:", this.x);
42 typedef fubar fubar3 = (x:50);
44 # note: a subclass gets its own copy of the class's global space. The
45 # original superclass constructor is not called when we refine it so we
46 # must decide whether to call it ourselves or not.
48 subclass fubar as sfubar {
49 refine public global constructor method void blah99()
52 "global constructor called (sfubar), gx should be 1:", this.gx);
53 super.blah99(); # sets gx to 2
54 ++this.gx; # should be 3 now
67 stdio.stdout->show("There should be 2 global constructor calls");
68 ++sfubar.gx; # should be 4 now
69 stdio.stdout->show("The value of gx in fubar should be 2: ",
71 stdio.stdout->show("The value of gx in sfubar should be 4: ",
73 stdio.stdout->show("there should be 5 constructor "
74 "and 5 destructor calls");
75 stdio.stdout->show("a.x should be 6: ",
77 stdio.stdout->show("Results should be 6, 5, 51, 11, and 1");