[fpc-pascal] Clone a instance of object

Sven Barth pascaldragon at googlemail.com
Mon Sep 23 14:36:26 CEST 2013


Am 23.09.2013 14:23, schrieb patspiper:
> On 23/09/13 14:57, Michael Van Canneyt wrote:
>>
>>
>> On Mon, 23 Sep 2013, Mattias Gaertner wrote:
>>
>>> On Mon, 23 Sep 2013 11:46:54 +0200 (CEST)
>>> Michael Van Canneyt <michael at freepascal.org> wrote:
>>>
>>>>
>>>>
>>>> On Mon, 23 Sep 2013, Zaher Dirkey wrote:
>>>>
>>>>>
>>>>> On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt 
>>>>> <michael at freepascal.org> wrote:
>>>>> Var
>>>>> C : TBaseObjectClass;
>>>>> begin
>>>>> C:=TBaseObjectClass(ClassType);
>>>>> Result:=C.Create;
>>>>> end;
>>>>>
>>>>>
>>>>> ​Yes, that help me, thanks.
>>>>>
>>>>> Now about assigning I must repeat it (assign) for every child 
>>>>> class copy its field members, is there any trick to copy the 
>>>>> values like assigning record var to another record
>>>>> var?
>>>>> I know it is kind of strange way :P
>>>>
>>>> There is no such safe mechanism.
>>>
>>> Well, actually there is a safe mechanism. You use a record to store the
>>> class variables. But it looks clumsily:
>>>
>>> type
>>> TMyClass = class(TPersistent)
>>> private
>>> type
>>> TMyClassVars = record
>>> a,b,c: string;
>>> end;
>>> private
>>> F: TMyClassVars;
>>> public
>>> procedure Assign(Source: TPersistent);
>>> property a: string read F.a write F.a;
>>> end;
>>>
>>> procedure TMyClass.Assign(Source: TPersistent);
>>> begin
>>> F:=TMyClass(Source).F;
>>> inherited;
>>> end;
>>
>> Yes. But that works only for 'simple' types and still requires you to 
>> write code.
>>
>> For the more general case where you can have arrays, classes, 
>> interfaces and whatnot as fields of your object, there is simply no 
>> correct way.
>
> Can't the clipboard help in cloning? Similar to the IDE's copy and paste.
Even then you'd need to convert the fields to the format the clipboard 
expects. And if you managed to correctly "stream" all properties than 
you don't need the clipboard anyway...

Regards,
Sven



More information about the fpc-pascal mailing list