Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-09

Object constructor

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Xiangrong Fang

Hi All,

Could anyone please explain this:

5.4 Constructors and destructors
<http://www.freepascal.org/docs-html/ref/ref.html#QQ2-64-77> As can be seen in the syntax diagram for an object declaration, Free Pascal supports constructors and destructors. The programmer is responsible for calling the constructor and the destructor explicitly when using objects.

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?

Thanks,
Xiangrong

Reply by email

Michael Van Canneyt
to Xiangrong Fang
Xiangrong Fang wrote 11 lines
Hi All, Could anyone please explain this: 5.4 Constructors and destructors As can be seen in the syntax diagram for an object declaration, Free Pascal supports constructors and destructors. The programmer is responsible for calling the constructor and the destructor explicitly when using objects. 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.

Similarly, the compiler knows that the memory allocated by the constructor must be freed again in the destructor.

Michael.

Reply by email

Xiangrong Fang
to Michael Van Canneyt
Show quoted text 13 lines
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.

Reply by email

Michael Van Canneyt
to Xiangrong Fang
Xiangrong Fang wrote 8 lines
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.

Ah.

You are using TP-style objects. Forget what I said then. What I wrote applies to classes.

My apologies for the misundestanding.

Michael.

Reply by email

Frederic Da Vitoria
to Xiangrong Fang: Well, could you please explain what's the difference between p1 and p2 in the program…

2013/9/17 Xiangrong Fang

Show quoted text 45 lines
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.

(Thanks to Michael for spotting that you are using TP objects)

A little bit below the citation in your first post, you'll find this: "A constructor/destructor pair is required if the object uses virtual methods. The reason is that for an object with virtual methods, some internal housekeeping must be done: this housekeeping is done by the constructor" :-) In Turbo Pascal, it seems that some VMT housekeeping was also done in the destructor, I don't know if this is still true in fpc.

Frederic Da Vitoria (davitof) Membre de l'April - « promouvoir et défendre le logiciel libre » - http://www.april.org

Reply by email

Florian Klämpfl
to Xiangrong Fang: Well, could you please explain what's the difference between p1 and p2 in the program…
Xiangrong Fang wrote 15 lines
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:

You cannot use fail outside of a constructor. So there is no proper error handling method available which does no create memory leaks.

Reply by email