DGL
https://delphigl.com/forum/

copying values between objects.
https://delphigl.com/forum/viewtopic.php?f=19&t=6608
Seite 1 von 1

Autor:  noeska [ So Mai 20, 2007 21:12 ]
Betreff des Beitrags:  copying values between objects.

How do i make an copy/clone of an object.
E.g. i want to copy vars from one existing object to another object.
This works as long as i do not free the object i copied from.
How do i make a real copy with values instead of references? So that i can free the object i copied from and continue using the object i copied to.

Thanks for your help in advance.

Autor:  Traude [ So Mai 20, 2007 23:22 ]
Betreff des Beitrags: 

Hallo Noeska,

Assume you have an object

Code:
  1. TMyObject = Class(TObject)
  2. Private
  3.    fVariable: Integer;
  4.    fList: TList;
  5.  
  6.    Procedure DoSomething;
  7. Public
  8.    Constructor Create(AVariable: Integer); Reintroduce;
  9.    Function GetVariable: Integer;
  10.    Procedure SetVariable(AVariable: Integer);
  11. End;


We assume you have an instance (FirstObject) of this class. Now you want to copy it.

First of all, it is necessary to create a new instance:
SecondObject:= TMyObject.Create(0);

Then you have to pass all the simple variables by
SecondObject := FirstObject;

But we are not finished yet. The object holds a TList, and this is an object, too. And not enough, it can be full of pointers.
By declaring "SecondObject := FirstObject" you DO NOT copy the content of the pointers, that means that the pointers of SecondObject.fList point to the same addresses as FirstObject.fList. As a consequence, if you free your SecondObject.fList the FirstObject.fList looses its memory too, BUT IS NOT INFORMED about that => a runtime crash is unavoidable.

That means, having pointers or objects with pointers where you order memory include always the responsibility for that memory. If you want to copy an object that has pointers with content you will have to copy this content explicitly.

N.B.: the above explanation may be not valid for NET.
Traude

Autor:  luketheduke [ Mo Mai 21, 2007 16:18 ]
Betreff des Beitrags: 

No, no, no, Traude. That creates exactly the problems noeska is facing.

@noeska: The VCL uses the very useful "Assign" Pattern for this:

Code:
  1.  
  2. type
  3. TMyPersistent = class
  4.   private
  5.     FOneValue: Integer;
  6.   public
  7.     OneReference: TObject;
  8.  
  9.     procedure Assign(mp: TMyPersistent);
  10. end;
  11.  
  12. procedure TMyPersistent.Assign(mp: TMyPersistent);
  13. begin
  14.   FOneValue := mp.FOneValue;
  15.   OneReference := mp.OneReference; //note: self now conatinas the exact same reference to whatever object mp contains. This object or wahtever is not copied!
  16. end;
  17.  


A lot of VCL Classes inherit from TPersistent, which introduces this behavior on a very general layer (in theory, you can copy the contents of any TPersistent instance to every other one. For further reference see the online help).

Autor:  Traude [ Mo Mai 21, 2007 17:49 ]
Betreff des Beitrags: 

@Luke: you are right. :oops:
@Noeska: sorry for the error. The mistake is in the line "SecondObject := FirstObject;" because objects are nothing but pointers and this statement would actually loose the new created second object. You have to copy the values inside the old object to the new one. As Luke said there are methods in TPersistent that can handle this problem: Assign and AssignTo. But if you have no TPersistent, you have to make it "by hand".

Autor:  noeska [ Mo Mai 21, 2007 19:03 ]
Betreff des Beitrags: 

Should i use Assign or AssignTo for this, i started writing AssignTo as i believe Assign is dependen on Assignto i believe, but i am not sure.

[EDIT]
Hmm the real problem was array of TMesh.
It should also have an Assign method. Also for the copy i should make new TMeshes and assign the values to these.
[/EDIT]

Autor:  luketheduke [ Di Mai 22, 2007 13:02 ]
Betreff des Beitrags: 

If you don't have objects, you have to do the assigning yourself.

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/