Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Do Mai 23, 2024 06:43

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 6 Beiträge ] 
Autor Nachricht
 Betreff des Beitrags: copying values between objects.
BeitragVerfasst: So Mai 20, 2007 21:12 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
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.

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: So Mai 20, 2007 23:22 
Offline
DGL Member
Benutzeravatar

Registriert: Di Okt 03, 2006 14:07
Beiträge: 1277
Wohnort: Wien
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


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mo Mai 21, 2007 16:18 
Offline
DGL Member
Benutzeravatar

Registriert: Fr Mai 14, 2004 18:56
Beiträge: 804
Wohnort: GER/OBB/TÖL-WOR/Greiling
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).

_________________
Bild

"User Error. Replace User and hit Continue."


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mo Mai 21, 2007 17:49 
Offline
DGL Member
Benutzeravatar

Registriert: Di Okt 03, 2006 14:07
Beiträge: 1277
Wohnort: Wien
@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".


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mo Mai 21, 2007 19:03 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
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]

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Di Mai 22, 2007 13:02 
Offline
DGL Member
Benutzeravatar

Registriert: Fr Mai 14, 2004 18:56
Beiträge: 804
Wohnort: GER/OBB/TÖL-WOR/Greiling
If you don't have objects, you have to do the assigning yourself.

_________________
Bild

"User Error. Replace User and hit Continue."


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 6 Beiträge ] 
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 3 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.055s | 15 Queries | GZIP : On ]