Quote
"Florian Sievert" <Phob...@T-Online.de> wrote in message
news:8es62h$u7c$1@news03.btx.dtag.de...
Hi Guys,
I have some problems with the following situation---
TYPE TEngineOBj=class(TObject)
TYPE TShip=class(TEngineObj);
TYPE TShip2=class(TEngineObj);
This are the objects I used! So the sesond and third are from TEngineObj.
Now I am using an array of TEngineObj
VAR OBJ:ARRAY[1..3] OF TEngineObj;
Then my program fills in some values into this array like:
BEGIN
OBJ[1]:=TShip1.Create;
OBJ[2]:=TShip2.Create;
END;
So now the problems comes ;-)
In one part of the program I have to move the content the whole OBJ[2] to
the first position in the array. The whole Object with all content, like the
put out of
the second place and overwrites the first.
BEGIN
OBJ[1]:=Obj[2];
END;
That was my try! In fact, if this arent objects it could be successful...
Instead the first object get the RAM adresse of the second and if I write
something to the first it also will be written into the second. And I dont
want this
because I have to kill the second object after this copy and on this way I
also will
kill the second...
Hope you understand what I try to explain and also hope that heres someone
who
can help me ;-)
As you found out, object variables are pointers to actual object instances
so Obj [x] := Obj [n] simply sets entry x to point to the same instance as
entry n. If you want to copy object instances you need to do what the VCL
does (see tPersistent) and create a method to do the copying, see below.
However, in the example you cite above you indicate that you free the
instance being copied after the copy so doing 'obj [1] := obj [2]; obj [2]
:= nil;' may accomplish what you want (no need to do obj [2].free
afterwards).
Type
tEngineObj = class (tObject)
. . .
public
procedure Assign (src : tEngineObj); virtual;
. . .
end;
tShip = class (tEngineObj)
. . .
public
procedure Assign (src : tEngineObj); override;
. . .
end;
procedure tEngineObj.Assign (src : tEngineObj);
begin
// copy src data to self
end;
procedure tShip.Assign (src : tEngineObj);
begin
inherited Assign (src);
// copy tShip specific src data to self
end;
ThanX in advise!
Sincely,
Florian
--
"Please excuse my terrible english! I know thats cruel, so please dont
laugh ;-)"