On Mon, 21 Aug 2006 14:30:06 +0000
Albert Zeyer <[EMAIL PROTECTED]> wrote:

> > > I think, RTTI is very powerfull and there should be a way to do
> > > the same as above without a virtual constructor. But is it clean?
> > 
> > To use RTTI you must derive from TPersistent or use the {$M+}
> > directive.
> > 
> >  
> > > In practice, I have a abstract base class with a function like
> > > this:
> > > 
> > > function TMyBase.CreateCopy(): TMyBase;
> > > begin
> > >   Result := Self.ClassType.Create(); // this doesn't work because
> > > ClassType is a TClass
> > >   // TODO: fill the result with same data ...
> > > end;
> > 
> > With TPersistent it would work like this
> >    TMyBase = class(TPersistent)
> >    public
> >      function CreateCopy: TMyBase;
> >      procedure Assign(Source: TPersistent); override;
> >    end;
> > 
> > it would be
> >  function TMyBase.CreateCopy: TMyBase;
> >  begin
> >    Result := TMyBaseClass(Self.ClassType).Create;
> >    Result.Assign(Self);
> >  end;
> > 
> > Every class must override the Assign method to copy all added
> > values.
> > 
> 
> But in this case, it would also not call the constructor of my
> inherited classes. 

Why not? Did you test it?


> The assignment of the data inside the object is
> not the problem, I have other virtual functions I can use for this.
> But it is important, that the constructor of my inherited class is
> also called because it does some important and needed init-work
> (which depends on the inherited class).
> 
> 
> > Another possibility:
> > Use TComponent as base class. Then you can copy without writing the
> > Assign procedures. The IDE uses this. It is much
> > slower than the Assign approach.
> > 
> 
> Performance is important in my case. I will have very much of this
> objects.
> 
> It seems that virtual constructors are the only possibility.


Mattias

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to