> > My question is, since the programmer is responsible for explicitly call >> object constructors, then why do we need constructors at all? In another >> word, what's the difference >> between an object constructor and an object method? >> > > Using the constructor tells the compiler that it should allocate memory on > the heap for a new instance, > and return a method to this new memory. There is then an implicit return > value for the constructor, and this is stored in the variable to which you > assign the result of the constructor. >
Well, could you please explain what's the difference between p1 and p2 in the program below: 1 program test; 2 type 3 TObj = object 4 public 5 Value: Integer; 6 procedure SetValue(AValue: Integer); 7 constructor Init(AValue: Integer); 8 end; 9 10 procedure TObj.SetValue(AValue: Integer); 11 begin 12 Value := AValue; 13 end; 14 15 constructor TObj.Init(AValue: Integer); 16 begin 17 Value := AValue; 18 end; 19 20 var 21 p1, p2 : ^TObj; 22 begin 23 New(p1); 24 p1^.SetValue(1); 25 WriteLn(p1^.Value); 26 New(p2, Init(2)); 27 WriteLn(p2^.Value); 28 end.
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal