Possible Duplicate:
Generics constructor with parameter constraint?
In my venture into Generics, I’ve now come to a dead end where I hope someone can point me to the hidden door 🙂
Given this type
type
TMyClass = class
private
FMyString: String;
FMyStringList: TStringList;
procedure SetMyString(const Value: String);
procedure SetMyStringList(const Value: TStringList);
public
constructor Create; overload;
constructor Create(InitString : String); overload;
destructor Destroy; override;
property MyString: String read FMyString write SetMyString;
property MyStringList : TStringList read FMyStringList write SetMyStringList;
end;
TMyClassList<T:TMyClass, constructor> = class(TObjectList<T>)
public
constructor Create; overload;
end;
implementation
{ TMyClass }
constructor TMyClass.Create;
begin
inherited;
MyString := '';
MyStringList := TStringList.Create;
MyStringList.Add('This');
MyStringList.Add('is');
MyStringList.Add('a');
MyStringList.Add('test.');
end;
constructor TMyClass.Create(InitString: String);
begin
Create;
MyString := InitString;
end;
I would like to be able to use both TMyClass constructors as needed.
constructor TMyClassList<T>.Create;
begin
Add(T.Create);
// Add(T.Create('test')); // <--- THIS FAILS ????
end;
But only the one without parameters is usable.
I forgot to mention – It does NOT fail … It will not COMPILE !!
Why is that?
Regards
Bimmer_R
You can’t specify general constructor constraints on generic types. All you can do is specify that they have a single parameterless constructor, as you have done. Work around it like this:
You’ll want to use a virtual constructor on
TMyClassto make this work as you intend.The other option is to use a parameterless constructor and use a separate routine for initialization.