On Mon, 21 Aug 2006 14:30:06 +0000 Albert Zeyer <[EMAIL PROTECTED]> wrote: > > 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?
Yes, I tested the following code:
// ------------------------------------
...
type
TBaseTestObject = class(TPersistent)
constructor Create();
procedure DoSomething(); virtual; abstract;
end;
TBaseTestClass = class of TBaseTestObject;
TTestObject = class(TBaseTestObject)
public
constructor Create();
destructor Destroy(); override;
procedure DoSomething(); override;
end;
TSkriptTestForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
mTest: TTestObject;
end;
var
SkriptTestForm: TSkriptTestForm;
implementation
procedure TSkriptTestForm.Button1Click(Sender: TObject);
var
cls: TBaseTestClass;
begin
cls := TTestObject;
mTest := cls.Create() as TTestObject;
mTest.DoSomething();
end;
constructor TBaseTestObject.Create();
begin
inherited Create();
WriteLn('TBaseTestObject Create');
end;
constructor TTestObject.Create();
begin
inherited Create();
WriteLn('TTestObject Create');
end;
destructor TTestObject.Destroy();
begin
WriteLn('TTestObject Destroy');
inherited Destroy();
end;
procedure TTestObject.DoSomething();
begin
WriteLn('TTestObject DoSomething');
end;
// ------------------------------------
TTestObject.Create won't ever be called (only TBaseTestObject.Create will be, but if I use TClass instead of TBaseTestClass, it also won't be). It also makes no difference if I use TPersistent as base or simply TObject. But the DoSomething call works nevertheless, therefore it seems, that the VMT (and other stuff) is initialized correctly, but my own constructor won't ever be called.
Albert