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.
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
OneReference := mp.OneReference;//note: self now conatinas the exact same reference to whatever object mp contains. This object or wahtever is not copied!
end;
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).
Registriert: Di Okt 03, 2006 14:07 Beiträge: 1277 Wohnort: Wien
@Luke: you are right.
@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".
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]
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.