[fpc-pascal] Clone a instance of object

Mattias Gaertner nc-gaertnma at netcologne.de
Mon Sep 23 12:29:49 CEST 2013


On Mon, 23 Sep 2013 12:10:41 +0200
Frederic Da Vitoria <davitofrg at gmail.com> wrote:

> 2013/9/23 Mattias Gaertner <nc-gaertnma at netcologne.de>
> 
> > 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;
> >
> 
> This wouldn't clone included objects. I mean: if the object to be cloned
> contains objects, those included objects should probably be cloned too, but
> putting them into a record wouldn't achieve that.

Anything that needs code to create needs code to clone.
You can always creates cases where cloning need special code. But if
you limit yourself you can use some of the existing frameworks like
TReader/TWriter of tiopf.

Mattias



More information about the fpc-pascal mailing list