summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/tbs/tb0156.pp
blob: a326cc14c59bc7acf9cbe0c70a03e5c831c4be48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{ %OPT=-Cr }

{ Old file: tbs0187.pp }
{ constructor in a WIth statement isn't called correct. (works at lest in the case stated)                    OK 0.99.11 (PM) }

type
        Tbaseclass = object
                base_arg : longint;
                st_count : longint;static;
                constructor     Init;
                destructor      Done;
                procedure       Run;                            virtual;

        end;
        Totherclass = object(Tbaseclass)
                other_arg : longint;
                procedure       Run;                            virtual;

        end;

const
    BaseRunCount : integer = 0;
    OtherRunCount : integer = 0;

constructor Tbaseclass.Init;

begin
  writeln('Init');
  Inc(st_count);
  Run;
end;

destructor Tbaseclass.Done;

begin
  writeln('Done');
  dec(st_count);
end;

procedure Tbaseclass.Run;

begin
  writeln('Base method');
  inc(BaseRunCount);
end;


procedure Totherclass.Run;

begin
  writeln('Inherited method');
  inc(OtherRunCount);
end;

 { try this as local vars }

 procedure test_local_class_init;
  var base1 : TbaseClass;
  var other1 : TOtherClass;
  begin
     with other1 do
          Init;
     with base1 do
          Init;
     with other1 do
        begin
           Writeln('number of objects = ',st_count);
           base_arg:=2;
           other_arg:=6;
           Run;
        end;
     { test if changed !! }

     if (other1.base_arg<>2) or (other1.other_arg<>6) then
       Halt(1);

     with base1 do
        begin
           Run;
           Done;
        end;
     other1.done;
   end;

var     base            : Tbaseclass;
        other           : Totherclass;
        testfield       : longint;

begin
// Uncommenting here and commenting the init in the WIth solves it.
//  Base.Init;
  with base do
  begin
    Init;
    Run;
    Done;
  end;
// Uncommenting here and commenting the init in the WIth solves it.
//  Other.init;
  with other do
  begin
    Init;
    Run;
    Done;
  end;

 test_local_class_init;
{ Calls Tbaseclass.Run when it should call Totherclass.Run }
  If (BaseRunCount<>4) or (OtherRunCount<>4) then
    Begin
       Writeln('Error in tb162');
       Halt(1);
    End;
end.