views:

262

answers:

4

In Delphi, if I have a simple class myClass and I pass an instance of it to a function with myFunction( myClass ), which is defined by function myFunction( myObject : myClass ) : Boolean...

  • Will a copy of myObject be made?

  • When I call methods of myObject in myFunction, will the original object be affected and modified too?

+10  A: 

Objects are passed by reference. A copy will not be made; there will be only one instance of the class, only one object. The original object is all there is.

Andreas Rejbrand
Darn you beat me to it... :) +1
Marjan Venema
Yeah, the original object is all there is on the heap. But you do get a **copy** of the reference, unless you pass by reference. In that case you get a reference to a reference to an object.
Wouter van Nifterick
[There can be only one](http://www.imdb.com/title/tt0091203/)
Svein Bringsli
+2  A: 

The objects are passed by reference.

Eugene Mayevski 'EldoS Corp
I beat you! :) -
Andreas Rejbrand
+5  A: 

There's a bit more to it than "objects are passed by reference." Objects are reference types, and so they're always passed around by reference by default. Any object variable is an object reference.

But if you pass an object variable "by reference," (to a var parameter,) then if you replace the object with a different object inside the routine you passed it to, you'll end up with a different object once the routine returns. Be careful when doing this; it can cause memory leaks if you change the only reference you have to a certain object.

Mason Wheeler
When you say you "replace the object with a different object," you really mean "replace the object *reference* with a *reference to* a different object." You can't replace an object with another object, but you can overwrite references all you want.
Rob Kennedy
@Mason and @Rob, you're both right in the sense that there is such a prevalent misnomer in calling "Object" a variable that is a reference to a `TObject`... `MyForm.Width` is the real width of the Form whose reference is held by the `MyForm` variable. You change the `MyForm` variable and `MyForm.Width` is the width of an another Form. So, "you'll end up with a different object" describes somewhat the programmer's reality in practice.
François
+7  A: 

In Delphi, objects are special pointers which refer to a data structure on heap memory. When you pass an object to a function, you are actually passing the pointer, not a copy of the whole object data. In this case, when you modify a field or property via that reference, it will affect the original object data. Here is a simple example demonstrating this behavior:

program ObjParamTest;

type
  TMyClass = class
  private
    FMyField : Integer;
  public
    property MyField : Integer read FMyField write FMyField;
  end;


function ModifyObject(AnObj: TMyClass);
begin
  AnObj.MyField := AnObj.MyField + 1;
end;  

var
  MyObj : TMyClass;
begin
  MyObj := TMyClass.Create;
  try
    AnObj.MyField := 2;
    Writeln(AnObj.MyField); // ==> Prints 2
    ModifyObject(MyObj);
    Writeln(AnObj.MyField); // ==> Prints 3
  finally
    MyObj.Free;
  end;
end.

Also take note, parameter modifiers (e.g. Var, Const, Out) only change the way the object reference is passed to the function, and have no effect on the original data structure.

Maybe this article clears up things about different ways of passing parameters to functions in Delphi for you more:

http://vcldeveloper.com/articles/different-function-parameter-modifiers-in-delphi/

Regards

vcldeveloper
Great linked article!
Mick
I agree with Mick that your article is well worth reading. Still, I think it would have been most beneficial to post a short, direct answer to the question here before sending to your post. Especially because it does not address in detail the differences between passing an object by value or by var.
François
Thanks, I added a short description and a simple example to the post.
vcldeveloper
+1. While all answers are technically correct, I believe this is the most helpfull one to OP.
Lieven