Hello.
Sorry for the misleading topic title, but I don't really know how to describe it. :? First of all, consider this code:
[pascal]
program Project1;
{$APPTYPE CONSOLE}
uses
FastMM4,
Classes,
SysUtils;
type
{ .: TClass1 :. }
TClass1 = class(TObject)
private
{ Private declarations }
FVal: Single;
FList: TList;
public
{ Public declarations }
constructor Create();
destructor Destroy(); override;
function AddIt(): TClass1;
property X: Single read FVal write FVal;
end;
{ .: TClass2 :. }
TClass2 = class(TClass1)
public
{ Public declarations }
constructor Create(const X: String);
procedure WriteVal();
end;
{ TClass1 }
function TClass1.AddIt: TClass1;
begin
Result := TClass1.Create();
FList.Add(Result)
end;
constructor TClass1.Create;
begin
inherited Create();
FList := TList.Create();
end;
destructor TClass1.Destroy;
var
I: Integer;
begin
for I := FList.Count -1 downto 0 do
TClass1(FList.Items[I]).Free();
FList.Clear();
FList.Free();
inherited Destroy();
end;
{ TClass2 }
constructor TClass2.Create(const X: String);
begin
inherited Create();
writeln('From class2: ', X);
end;
procedure TClass2.WriteVal;
begin
writeln(X:0:2);
end;
var
C: TClass1;
begin
C := TClass1.Create();
try
C.X := 3.14;
with TClass2(C.AddIt()) do
begin
X := 5.25;
WriteVal();
end;
finally
FreeAndNil(C);
end;
readln;
end.
[/pascal]
Now I'd like to know how to call the TClass2 constructor using that casting:
[pascal]
with TClass2(C.AddIt()) do
[/pascal]
Is it possible? :?
Thank you in advance and I hope it's clear for all.
Bookmarks