I get E2010 Incompatible types: 'TDerivedFrame' and 'TBaseFrame'
on the following code:
type
TBaseFrame = class(TFrame)
end;
TDerivedFrame = class(TBaseFrame)
end;
TContainer<T: TBaseFrame> = class
private
FFrame: T;
public
property Frame: T read FFrame;
constructor Create;
end;
constructor TContainer<T>.Create;
begin
inherited;
FFrame := TBaseFrame(T).Create(nil);
end;
var
FTab: TContainer<TDerivedFrame>;
Using only T.Create(nil)
causes E2568 Can't create new instance without CONSTRUCTOR constraint in type parameter declaration
.
As far as I know you can only create a constructor constraint with a parameterless constructor.
What is the correct way to do this?
PS: The code compiles when I remove the variable which makes me think this is a compiler error?!
Edit: I understand the E2010 error, but even with T(TBaseFrame(T).Create(nil))
it doesn't work. This compiles, but causes an access violation at runtime:
type
TBaseFrame = class(TFrame)
public
constructor Create(AOwner: TComponent); override;
end;
TDerivedFrame = class(TBaseFrame)
public
constructor Create(AOwner: TComponent); override;
end;
TContainer<T: TBaseFrame> = class
private
FFrame: T;
public
property Frame: T read FFrame;
constructor Create;
end;
constructor TBaseFrame.Create(AOwner: TComponent);
begin
inherited;
end;
constructor TDerivedFrame.Create(AOwner: TComponent);
begin
inherited;
end;
constructor TContainer<T>.Create;
begin
inherited;
FFrame := T(TBaseFrame(T).Create(nil));
end;
var
FTab: TContainer<TDerivedFrame>;
begin
FTab := TContainer<TDerivedFrame>.Create;
end.