The "Delphi Way" of copying objects in an OOP fashion is
often to use the AssignTo method.

Most of the the VCL objects will have virtual AssignTo methods
already - and they will allow you to assign the value of one
object to another.

    mySourceObject.AssignTo( myTargetObject);

This is NOT the same as pointing to the same object - you
construct an "empty" myTargetObject (or use one you already
have handy) and then this method copies stuff from mySourceObject.

When you derive your own object from a base that already has
an AssignTo method you should override it.  That new virtual
AssignTo usually calls the base class AssignTo to copy the
inherited properties and then explicitly handles copying
stuff specific to the derived class itself.

AssignTo is (sort of) the Delphi way of doing what C++ calls
a "copy constructor".    (An important difference is that they
don't get called automatically when you pass objects by value).

hope this is useful

-ns





----- Original Message -----
From: "Ross Levis" <[EMAIL PROTECTED]>
To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]>
Sent: Wednesday, August 15, 2001 4:20 AM
Subject: [DUG]: Duplicate an Object & Data


> Is it possible to duplicate a static object with it's contents.  I don't
> want 2 pointers to the same object.
>
> Say I have an object defined like this:
>
> type TTest = class
>   TestName: String;
>   TestArray: Array of Real;
>   TestList: TStringList;
> end;
>
> var Test,Test2: TTest;
>
> Later a TTest is created and assigned to Test and data allocated to the
> fields.  Is there a simple way of duplicating the data and assigning it to
> Test2 without having to copy each field separately?
>
> Cheers,
> Ross.
> --------------------------------------------------------------------------
-
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
>

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to