Board index » delphi » Copying contents of an object to another object of the same type

Copying contents of an object to another object of the same type

I would like to copy an object to another object of the same type.

Something like Object1 := Object2; doesn't work, because it only lets
Object1 point to the data of Object2. If I would free Object2, Object1
would be freed too.

Anyone knows a solution?

Thanx in advance,
Ritchie

 

Re:Copying contents of an object to another object of the same type


    The Assign method will often do what you want here.

    There is no perfect solution that will always do what you want.
There cannot be, because saying "copy the contents of the object"
doesn't specify what you want to do if part of the contents consists
of other pointers (eg other objects) - do you want to copy those
pointers or make new copies of what the pointers point to?

Quote
Ritchie wrote:
> I would like to copy an object to another object of the same type.

> Something like Object1 := Object2; doesn't work, because it only lets
> Object1 point to the data of Object2. If I would free Object2, Object1
> would be freed too.

> Anyone knows a solution?

> Thanx in advance,
> Ritchie

Other Threads