Board index » delphi » Why use a dynamics object?

Why use a dynamics object?

Hi all,

I have a little question.  Suppose there is a program declaration as:

type
  PMyObj = ^TMyObj;
  TMyObj = object
    constructor Init;
    blah...
  end;

My question is:
1. What's the possible reason a programmer use a dynamic object instead
the static one?

2. Is there any fundamental different in their Init constructor?

3. Why put "PMyObj = ^TMyObj" before the object declaration?  And why put
it at all?  Isn't that simpler to just declare "var MyObj:^TMyObj"?

Thanks alot before...

--
-Eddy

 

Re:Why use a dynamics object?


In article <DICu5K...@uns.bris.ac.uk>, ew4...@kestrel.fen.bris.ac.uk (Eddy) says:
Quote

>Hi all,

>I have a little question.  Suppose there is a program declaration as:

>type
>  PMyObj = ^TMyObj;
>  TMyObj = object
>    constructor Init;
>    blah...
>  end;

>My question is:
>1. What's the possible reason a programmer use a dynamic object instead
>the static one?

If you allocate it on the heap, you have more room for your variables.
In TPW/BP f.W. you have a max amount (actually you'll have less) of 64KB
for static global variables.  On the heap, where it goes when it's
dynamic, you generally have several meg.

Quote
>2. Is there any fundamental different in their Init constructor?

None.  In the constructor you do not need to know whether it's
dynamic or static.

Quote
>3. Why put "PMyObj = ^TMyObj" before the object declaration?  And why put
>it at all?  Isn't that simpler to just declare "var MyObj:^TMyObj"?

Clarity.  You could put it after the TMyObj declaration as well - that's
a matter of style if you do not need access to PMyObj in the object
itself.  As OWL declares them before, it is generally done before.
if you declare
var MyObj :  ^TMyObj;
you will never have access to the pointer type, so if you need to pass
the object to a procedure you will have a hard time doing so.

I hope this helps,
Jochen

________________________________________________________________________
joch...@berlin.snafu.de                        71270.3...@compuserve.com
I post, therefore I am.

Re:Why use a dynamics object?


Quote
ew4...@kestrel.fen.bris.ac.uk (Eddy) wrote:
>My question is:
>1. What's the possible reason a programmer use a dynamic object instead
>the static one?

Consider the TStream (TDosStream,TMemStream,TEmsStream,etc.) objects.
I can write one routine that uses data to/from TStream and later at
run-time, I can decide which to use -- if I have enough EMS, use
TEmsStream, if not use TMemStream, and so on...

It's also better to use for larger projects. Remember you are still
limited to 64K data & stack. You can allocate objects from the heap...

Quote
>2. Is there any fundamental different in their Init constructor?

>3. Why put "PMyObj = ^TMyObj" before the object declaration?  And why put
>it at all?  Isn't that simpler to just declare "var MyObj:^TMyObj"?

I'm not sure about this but... Try this:

var
 Obj1 : ^TMyObj;
 Obj2 : ^TMyObj;

Obj1:=Obj2;

I don't think the compiler likes these things... You will have to type
cast Obj1 or Obj2 into a Pointer.

You also cannot use "^" inside a procedure/function header. Ie. You
cannot do:

 procedure SaveTemp(P : ^TStream);

-----------------------------
Jonathan L. Zarate  
http://www.mailhost.net/~jlz/

Re:Why use a dynamics object?


Quote
>Hi all,
>I have a little question.  Suppose there is a program declaration as:
>type
>  PMyObj = ^TMyObj;
>  TMyObj = object
>    constructor Init;
>    blah...
>  end;
>My question is:
>1. What's the possible reason a programmer use a dynamic object instead
>the static one?

Dynamic methods work much like Virtual methods, but take up less space (2
bytes less I think) and are the only really good way to do automatic
message-passing (Windows), otherwise you'd have to build a table of
message-handling procedures in the object.

Quote
>2. Is there any fundamental different in their Init constructor?

It would do the same sort of thing as a constructor for an object with virtual
methods, I think.

Quote
>3. Why put "PMyObj = ^TMyObj" before the object declaration?  And why put
>it at all?  Isn't that simpler to just declare "var MyObj:^TMyObj"?

Look at the way you do windows and you'll see : you frequently create objects
using New, e.g.
    MainWindow := New( PWindow, Init( blah, think, blob ) );
So you need a predefined pointer type.  Also, you may want to pass the pointer
of an object (for linked lists of objects, etc.), and BP's nice type checking
will want you to have a specific pointer type, otherwise (if you just passed a
variable of type Pointer) you would have to do something messy like
   ObjectType(PassedObject^).Thing
instead of
   PassedObject^.Thing
You will find that there are some places where you want a direct object but
many that you need to use pointers.
Quote
>Thanks alot before...
>--
>-Eddy

Re:Why use a dynamics object?


On 21 Nov 1995, Jochen Heyland wrote:

Quote
> Date: 21 NOV 1995 08:29:12 GMT
> From: Jochen Heyland <joch...@berlin.snafu.de>
> Newgroups: comp.lang.pascal.borland
> Subject: Re: Why use a dynamics object?

> In article <DICu5K...@uns.bris.ac.uk>, ew4...@kestrel.fen.bris.ac.uk (Eddy) says:

> >Hi all,

> >I have a little question.  Suppose there is a program declaration as:

> >type
> >  PMyObj = ^TMyObj;
> >  TMyObj = object
> >    constructor Init;
> >    blah...
> >  end;

> >My question is:
> >1. What's the possible reason a programmer use a dynamic object instead
> >the static one?
> If you allocate it on the heap, you have more room for your variables.
> In TPW/BP f.W. you have a max amount (actually you'll have less) of 64KB
> for static global variables.  On the heap, where it goes when it's
> dynamic, you generally have several meg.

> >2. Is there any fundamental different in their Init constructor?
> None.  In the constructor you do not need to know whether it's
> dynamic or static.

> >3. Why put "PMyObj = ^TMyObj" before the object declaration?  And why put
> >it at all?  Isn't that simpler to just declare "var MyObj:^TMyObj"?
> Clarity.  You could put it after the TMyObj declaration as well - that's
> a matter of style if you do not need access to PMyObj in the object
> itself.  As OWL declares them before, it is generally done before.
> if you declare
> var MyObj :  ^TMyObj;
> you will never have access to the pointer type, so if you need to pass
> the object to a procedure you will have a hard time doing so.

> I hope this helps,
> Jochen

> ________________________________________________________________________
> joch...@berlin.snafu.de                        71270.3...@compuserve.com
> I post, therefore I am.

It's a good step to declare type PmyObj=^TMyObj because if you want to make
this type into a parameter of a function or procedure, you are save from
error message.
eg procedure TMyObj.doSomething(MyVar:PMyObj);
instead of
 procedure TMyObj.doSomething(MyVar:^TmyObj);
which will cause error.

Me,
Salim.

Other Threads