Help! need your advice.
Help! need your advice.
I want to declare a genric class refrence type as like this, but XE3 could not.
THashClass = class of THash;
THash = class;
The error is the following;
[dcc32 Error] gHash.pas(31): E2029 ';' expected but '<' found
How to solve the problem ?
I want to declare a genric class refrence type as like this, but XE3 could not.
THashClass = class of THash
THash
The error is the following;
[dcc32 Error] gHash.pas(31): E2029 ';' expected but '<' found
How to solve the problem ?
You cannot have a class reference to a generic class.
ReplyDeleteWhy do you need it anyway?
Stefan Glienke Sorry for my english. I'd like to have a class function which returns itself. Like this...
ReplyDeletetype
TBobJenkinsClass = class of TBobJenkins;
TBobJenkins = class
private
class var
FHashValue: Integer;
public
class function Hash(const ASrc: TStream; InitData: Integer = 0): TBobJenkinsClass; overload;
class function Hash(const AValue: String; InitData: Integer = 0): TBobJenkinsClass; overload;
class function HashFromFile(const AFileName: String; InitData: Integer = 0): TBobJenkinsClass;
class function AsString: String;
class property AsInteger: Integer read FHashValue;
end;
Stefan Glienke Oh. Thank you for your answer :)
ReplyDeleteI guess the only possibility would be to let the generic class inherit from a non generic one on which you can declare a class reference.
ReplyDeleteStefan Glienke Thank you :)
ReplyDeleteMichael Jung Why not this:
ReplyDeleteprogram Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
TBobJenkinsRoot = class
end;
TBobJenkinsClass = class of TBobJenkinsRoot;
TBobJenkins = class(TBobJenkinsRoot)
private
class var
FHashValue: Integer;
public
class function Hash(const AValue: String; InitData: Integer = 0): TBobJenkinsClass; overload;
class property AsInteger: Integer read FHashValue;
end;
{ TBobJenkins }
class function TBobJenkins.Hash(const AValue: String;
InitData: Integer): TBobJenkinsClass;
begin
Result := TBobJenkins;
end;
var
Cl : TBobJenkinsClass;
begin
try
Cl := TBobJenkins.Hash('123');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
François Piette That is what I meant, however there might be a problem with accessing the class var because these are not virtual and actually some members have to be moved to the base class because otherwise there is nothing to do with your metaclass.
ReplyDeleteTHashClass = class of THash
ReplyDelete