Board index » delphi » Two questions on Constructors/Destructors usage (in FreePascal)

Two questions on Constructors/Destructors usage (in FreePascal)

Hi all,

I used a lot of Pascal in the past. Now I started to write a program
with FreePascal and found out about object oriented programming. I think
it is great (!), but I would like some help on details about
Constructors and Destructors and their use:

1. Obviously you should destruct what you construct when you don't need
it anymore. But if I have a function which returns an object, I
construct it at the beginning (and now I know I have to) but when do I
destruct it? It is al taken care by the compiler or what?
For example:

function myfunction(myarg:Real):myclass;
begin
  myfunction:=myclass.init;  //init is a constructor of class myclass
  myfunction.afunctionofmyclass(myarg);
end;

begin
  myobject:=myclass.init;
  myarg:=5;
  myobject:=myfunction(myarg);
  ...
  myobject.done;
end.

So, I constructed the object myobject and destructed it. BUT I created
the object myfunction but never destroyed it... Is this correct? (Well,
it works, but I wonder if I leave a lot of garbage...)

2. This one is somewhat related: Can I use the Constructor more than
once for the same object? For example:

begin
  myobject:=myclass.init(1);
  ...
  myobject:=myclass.init(2);
  ...
  myobject.done;
end.

Thanks in advance

Manuel Correa

--
 Everything should be made as simple as possible, but not
simpler. Albert Einstein

 

Re:Two questions on Constructors/Destructors usage (in FreePascal)


why do you construct the object twice??

first you construct the object with the following line:

    myobject := myclass.init;

then your MyFunction (MyArg:Real): MyClass; function constructs also a
object.

when you used the following line:
    myobject:=myfunction(myarg);

this creates a new object (done in your myfunction function) and replaces
the old object (which surely gets lost, and cannot
be used anymore)..

but if you want initailize a object using a function, you just leave the
following line out:

    myobject := myclass.init;

since it will be initialized in your function.

Peter Out (developer of GWO ,G2). http://www.phibian.net/g2preview.jpg
http://www.phibian.net/g2preview_win.jpg
http://www.phibian.net/Downloads/g2rc210.zip (first release).

Manuel Ritto Correa <mcor...@civil.ist.utl.pt> schreef in berichtnieuws
3ACB0D76.30DE...@civil.ist.utl.pt...

Quote
> Hi all,

> I used a lot of Pascal in the past. Now I started to write a program
> with FreePascal and found out about object oriented programming. I think
> it is great (!), but I would like some help on details about
> Constructors and Destructors and their use:

> 1. Obviously you should destruct what you construct when you don't need
> it anymore. But if I have a function which returns an object, I
> construct it at the beginning (and now I know I have to) but when do I
> destruct it? It is al taken care by the compiler or what?
> For example:

> function myfunction(myarg:Real):myclass;
> begin
>   myfunction:=myclass.init;  file://init is a constructor of class myclass
>   myfunction.afunctionofmyclass(myarg);
> end;

> begin
>   myobject:=myclass.init;
>   myarg:=5;
>   myobject:=myfunction(myarg);
>   ...
>   myobject.done;
> end.

> So, I constructed the object myobject and destructed it. BUT I created
> the object myfunction but never destroyed it... Is this correct? (Well,
> it works, but I wonder if I leave a lot of garbage...)

> 2. This one is somewhat related: Can I use the Constructor more than
> once for the same object? For example:

> begin
>   myobject:=myclass.init(1);
>   ...
>   myobject:=myclass.init(2);
>   ...
>   myobject.done;
> end.

> Thanks in advance

> Manuel Correa

> --
>  Everything should be made as simple as possible, but not
> simpler. Albert Einstein

Re:Two questions on Constructors/Destructors usage (in FreePascal)


Quote
"Santing, Peter" wrote:

> why do you construct the object twice??

> first you construct the object with the following line:

>     myobject := myclass.init;

> then your MyFunction (MyArg:Real): MyClass; function constructs also a
> object.

> when you used the following line:
>     myobject:=myfunction(myarg);

> this creates a new object (done in your myfunction function) and replaces
> the old object (which surely gets lost, and cannot
> be used anymore)..

> but if you want initailize a object using a function, you just leave the
> following line out:

>     myobject := myclass.init;

> since it will be initialized in your function.

Yes, that is what I wanted to do. Thank you.

Manuel Correa

Re:Two questions on Constructors/Destructors usage (in FreePascal)


Quote
In article <3ACC7C73.DC117...@civil.ist.utl.pt>, Manuel Ritto Correa wrote:
>"Santing, Peter" wrote:

>> why do you construct the object twice??
>> since it will be initialized in your function.

>Yes, that is what I wanted to do. Thank you.

I often found it handy to think of classes as pointers to records for this
kind of cases. (which they in fact are, with some extra's)

Re:Two questions on Constructors/Destructors usage (in FreePascal)


Quote

> Yes, that is what I wanted to do. Thank you.

glad to be of any help.

Peter Out!

Re:Two questions on Constructors/Destructors usage (in FreePascal)


Marco van de Voort wrote:

Quote

> In article <3ACC7C73.DC117...@civil.ist.utl.pt>, Manuel Ritto Correa wrote:
> >"Santing, Peter" wrote:

> >> why do you construct the object twice??
> >> since it will be initialized in your function.

> >Yes, that is what I wanted to do. Thank you.

> I often found it handy to think of classes as pointers to records for this
> kind of cases. (which they in fact are, with some extra's)

Yes. I see what you mean. That was exactly my problem, because although
I programmed in Turbo Pascal some years ago I never used pointers.
So, when returning to Pascal, I got FreePascal and seeing in the
reference manual those concepts about OOP, I was enthusiastic about it.
However I had some difficulties distinguishing between the Object Type
and the Class Type.
But now, after some experiments, reading some tuttorials (mostly C++ I
am afraid) and after some help from a nice guy from canada who e-mailed
me, I think I understand the difference and what I am dealing with. :-)
Anyway, tell me if I am right: either you use the Class type or the
Object type, not both of them because it gets messy, right ?

Manuel Correa

--
 Everything should be made as simple as possible, but not
simpler. Albert Einstein

Re:Two questions on Constructors/Destructors usage (in FreePascal)


Quote
In article <3ACD95A4.A68E6...@civil.ist.utl.pt>, Manuel Ritto Correa wrote:
>Marco van de Voort wrote:

>> In article <3ACC7C73.DC117...@civil.ist.utl.pt>, Manuel Ritto Correa wrote:
>But now, after some experiments, reading some tuttorials (mostly C++ I
>am afraid) and after some help from a nice guy from canada who e-mailed
>me, I think I understand the difference and what I am dealing with. :-)
>Anyway, tell me if I am right: either you use the Class type or the
>Object type, not both of them because it gets messy, right ?

The class type more or less supersedes the Object type in FPC.
For new code there isn't much reason to use Object except BP compability.

So I would simply stick to Object if it has to be compatible with BP, and
class otherwise (Class is source compatible to Delphi btw)

Re:Two questions on Constructors/Destructors usage (in FreePascal)


Marco van de Voort wrote:

Quote
> >Anyway, tell me if I am right: either you use the Class type or the
> >Object type, not both of them because it gets messy, right ?

> The class type more or less supersedes the Object type in FPC.
> For new code there isn't much reason to use Object except BP compability.

> So I would simply stick to Object if it has to be compatible with BP, and
> class otherwise (Class is source compatible to Delphi btw)

OK. Thanks for making that clear. I see that you are part of the
FreePascal team, so I suggest that maybe that remark should be
incorporated in the reference manual. At least for those guys like me
that are learning OOP with FreePascal...

Manuel Correa

--
 Whenever you find yourself on the side of the majority, it is
time to pause and reflect. Mark Twain

Re:Two questions on Constructors/Destructors usage (in FreePascal)


Quote
In article <3ACDA803.C0EA5...@civil.ist.utl.pt>, Manuel Ritto Correa wrote:
>Marco van de Voort wrote:

>> >Anyway, tell me if I am right: either you use the Class type or the
>> >Object type, not both of them because it gets messy, right ?

>> The class type more or less supersedes the Object type in FPC.
>> For new code there isn't much reason to use Object except BP compability.

>> So I would simply stick to Object if it has to be compatible with BP, and
>> class otherwise (Class is source compatible to Delphi btw)

>OK. Thanks for making that clear. I see that you are part of the
>FreePascal team, so I suggest that maybe that remark should be
>incorporated in the reference manual. At least for those guys like me
>that are learning OOP with FreePascal...

I'll sent your remark to the doc maintainer, and I agree, if it is not
already in it, it should get in it.

Some other remark: In some very rare cases the object model can be faster.

This because dynamic allocation is relatively slow (compared to local
variables, but in general it is not worth the trouble.

Other Threads