元类默认参数值(Delphi 2009)
Metaclass default argument value (Delphi 2009)
我想在元类参数中给出默认值:
type
TMyClass = class
end;
type
TMyClassMetaClass = class of TMyClass;
procedure MyProcedure(const AMetaClass: TMyClassMetaClass = TMyClass);
有可能吗?在 Delphi2009 中,它给了我错误:E2026 预期常量表达式
根据语言规则,元类不是常量表达式。所以最好的办法是改用重载:
procedure MyProcedure(const AMetaClass: TMyClassMetaClass); overload;
procedure MyProcedure; overload;
并在实施中:
procedure MyProcedure(const AMetaClass: TMyClassMetaClass);
begin
....
end;
procedure MyProcedure;
begin
MyProcedure(TMyClass);
end;
我想在元类参数中给出默认值:
type
TMyClass = class
end;
type
TMyClassMetaClass = class of TMyClass;
procedure MyProcedure(const AMetaClass: TMyClassMetaClass = TMyClass);
有可能吗?在 Delphi2009 中,它给了我错误:E2026 预期常量表达式
根据语言规则,元类不是常量表达式。所以最好的办法是改用重载:
procedure MyProcedure(const AMetaClass: TMyClassMetaClass); overload;
procedure MyProcedure; overload;
并在实施中:
procedure MyProcedure(const AMetaClass: TMyClassMetaClass);
begin
....
end;
procedure MyProcedure;
begin
MyProcedure(TMyClass);
end;