| View previous topic :: View next topic |
| Author |
Message |
Gerhard Venter Guest
|
Posted: Tue Nov 18, 2003 6:17 pm Post subject: how to specify an object as a parameter of a procedure |
|
|
When you pass an object as a parameter of a procedure, and in the procedure
you will change a property of the object do you specify the Object parameter
as var or as const or nothing?
I know that even if it is specified as const you can still change the
properties of the object but what I want to know is why or in what case
would you specify an object parameter as const? Do you need to specify it at
all? (because objects are dynamic pointers in Object Pascal and is it
implied that they are var?
Thanks
Gerhard
|
|
| Back to top |
|
 |
Eric Hill Guest
|
Posted: Tue Nov 18, 2003 6:38 pm Post subject: Re: how to specify an object as a parameter of a procedure |
|
|
| Quote: | When you pass an object as a parameter of a procedure, and in the
procedure
you will change a property of the object do you specify the Object
parameter
as var or as const or nothing?
|
Typically nothing. An object variable is a special type of pointer under
the hood. You're basically passing a pointer to the object into the
routine.
| Quote: | I know that even if it is specified as const you can still change the
properties of the object but what I want to know is why or in what case
would you specify an object parameter as const?
|
"const" parameters are used so the compiler can do a few optimizations
(like on string handling) and to ensure that you don't change the value
passed into the routine. Using a "const" on an object pointer ensures that
you can't change the pointer, but doesn't specify anything about the
contents (the object) of the pointer. As a general rule, you shouldn't
pass objects with the "const" parameter.
| Quote: | Do you need to specify it at
all? (because objects are dynamic pointers in Object Pascal and is it
implied that they are var?
|
Just realize that an object passed into a routine without the "var" keyword
isn't a "var" because you're not changing the pointer, you're changing the
values of the object that the pointer points to. <g>
Eric
|
|
| Back to top |
|
 |
Gerhard Venter Guest
|
Posted: Tue Nov 18, 2003 7:03 pm Post subject: Re: how to specify an object as a parameter of a procedure |
|
|
Thanks Eric (second time you have helped me to clarify my own
interpretations, previously with RO ). It is like looking at a word
sometimes and the spelling looks wrong but yet when you look it up it is
correct. And then there is the English language - not a very descriptive
language like many others :)
One more question : So if an object has the "var" keyword does it mean that
I can change the pointer? If that is true what will be an example when you
want to do that for instance?
By the way I decided to and have written an Indy framework with persistent
connections for my messaging project.
Gerhard
"Eric Hill" <eric (AT) ijack (DOT) net> wrote
| Quote: | When you pass an object as a parameter of a procedure, and in the
procedure
you will change a property of the object do you specify the Object
parameter
as var or as const or nothing?
Typically nothing. An object variable is a special type of pointer under
the hood. You're basically passing a pointer to the object into the
routine.
I know that even if it is specified as const you can still change the
properties of the object but what I want to know is why or in what case
would you specify an object parameter as const?
"const" parameters are used so the compiler can do a few optimizations
(like on string handling) and to ensure that you don't change the value
passed into the routine. Using a "const" on an object pointer ensures
that
you can't change the pointer, but doesn't specify anything about the
contents (the object) of the pointer. As a general rule, you shouldn't
pass objects with the "const" parameter.
Do you need to specify it at
all? (because objects are dynamic pointers in Object Pascal and is it
implied that they are var?
Just realize that an object passed into a routine without the "var"
keyword
isn't a "var" because you're not changing the pointer, you're changing the
values of the object that the pointer points to.
Eric
|
|
|
| Back to top |
|
 |
Mauricio Magni Guest
|
Posted: Tue Nov 18, 2003 7:04 pm Post subject: Re: how to specify an object as a parameter of a procedure |
|
|
Hello Eric
"Eric Hill" <eric (AT) ijack (DOT) net> wrote
| Quote: | As a general rule, you shouldn't
pass objects with the "const" parameter.
|
I usually pass objects as const, in order to avoid confuse my self and
assign a new object reference to the parameter passed. I think there is no
cost associated with const parameters. Can you explain me why I shouldn't do
this?
Thanks a lot.
Mauricio.
|
|
| Back to top |
|
 |
Eric Hill Guest
|
Posted: Tue Nov 18, 2003 8:25 pm Post subject: Re: how to specify an object as a parameter of a procedure |
|
|
| Quote: | One more question : So if an object has the "var" keyword does it mean
that
I can change the pointer? If that is true what will be an example when
you
want to do that for instance?
|
The use of var to pass an object back is not very common, but is useful in
some situations. If you need to have a routine instantiate many variables
at once, this makes more sense:
procedure MakeInstances(var ObjLeft, ObjRight, ObjTop, ObjBottom: TSide);
It's not a great example though as you would more commonly have a TBox
object that has those four sides as properties :)
| Quote: | By the way I decided to and have written an Indy framework with
persistent
connections for my messaging project.
|
Cool. I just started using the NexusDB transport system for just such a
need - it has been designed around messaging from the ground up so it lends
itself rather well to that functionality :)
Eric
|
|
| Back to top |
|
 |
Eric Hill Guest
|
Posted: Tue Nov 18, 2003 8:27 pm Post subject: Re: how to specify an object as a parameter of a procedure |
|
|
| Quote: | I usually pass objects as const, in order to avoid confuse my self and
assign a new object reference to the parameter passed. I think there is
no
cost associated with const parameters. Can you explain me why I shouldn't
do
this?
|
I can't tell you you /shouldn't/ do this since it's not hurting you. <g>
I can only tell you that it's /more common/ to use the (AObject: TObject)
syntax over (const AObject: TObject).
Eric
|
|
| Back to top |
|
 |
|