Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clone a existing object?

3,265 views
Skip to first unread message

Tony Caduto

unread,
Apr 20, 2008, 8:19:35 PM4/20/08
to
Is using a tmemorystream the best way to do this, or is there some other
thing I may not know about in the RTTI functions
etc for cloning a existing object to a newly created one?

Thanks,

Tony

Gordos Colos

unread,
Apr 20, 2008, 10:04:35 PM4/20/08
to
Tony Caduto wrote:

Get the source of TSimpleGraph and see how it's done there.

http://delphiarea.com/products/simplegraph/

GC

Hans-Peter Diettrich

unread,
Apr 20, 2008, 9:54:31 PM4/20/08
to

A general "copy constructor" doesn't exist, but some components support
an Assign method.

DoDi

Serge Dosyukov (Dragon Soft)

unread,
Apr 21, 2008, 11:54:01 AM4/21/08
to
Depend on what exactly are you trying to achieve.
At run-time copy usually works using .Assign. One have to be careful since
not all objects, especially custom one, support it for all properties.
.Net has support for serialization, Delphi does not, but there are methods
and even components/classes which allow you serialize objects RTTI way.
You can do it yourself, or find code which will do it for you.

Some info from my experience is available here
http://blog.dragonsoft.us/2008/04/21/how-to-serialize-delphi-object/

"Tony Caduto" <tony....@gmail.com> wrote in message
news:480bdd94$1...@newsgroups.borland.com...

Michael C.

unread,
Apr 21, 2008, 3:40:48 AM4/21/08
to

There is no standard "easy way" in Delphi to clone an existing object.
The Delphi developers choose, in some cases, to provide an Assign method.

Some professional developers use a technique to save
an object to a stream and restore it later.
This is called serialization,
and is describe at:

http://en.wikipedia.org/wiki/Serialization

GrandmasterB

unread,
Apr 21, 2008, 5:09:11 PM4/21/08
to
"Tony Caduto" <tony....@gmail.com> wrote in message
news:480bdd94$1...@newsgroups.borland.com...
> Is using a tmemorystream the best way to do this, or is there some other
> thing I may not know about in the RTTI functions
> etc for cloning a existing object to a newly created one?

I generally add a cloning method to those classes who's objects I'll want to
copy during runtime. Something similar to a copy constructor. For example:

var
o1: TMyObject
o2: TMyObject
begin
o1 := TMyObject.Create;
o2 := o1.Clone();

or

o1 := TMyObject.Create;
o2 := TMyObject.Create;
o1.CopyTo( o2);

And then manually implement the method

function TMyObject.Clone(): TMyObject;
begin
result := TMyObject.Create;
result.field1 := field1;
result.field2 := field2;
//etc
end;

Eug. C.

unread,
Apr 21, 2008, 6:23:35 PM4/21/08
to

function CloneComponent(AAncestor: TComponent): TComponent;
var
XMemoryStream: TMemoryStream;
XTempName: string;
begin
Result:=nil;
if not Assigned(AAncestor) then exit;

XMemoryStream:=TMemoryStream.Create;
try
XTempName:=AAncestor.Name;
AAncestor.Name:='clone_' + XTempName;
XMemoryStream.WriteComponent(AAncestor);
AAncestor.Name:=XTempName;
XMemoryStream.Position:=0;
Result:=TComponentClass(AAncestor.ClassType).Create(AAncestor.Owner);
if AAncestor is TControl
then TControl(Result).Parent:=TControl(AAncestor).Parent;
XMemoryStream.ReadComponent(Result);
finally
XMemoryStream.Free;
end;
end;


0 new messages