delphi - 元类默认参数值 (Delphi 2009)

标签 delphi metaclass default-arguments

我想在元类参数中给出默认值:

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;

关于delphi - 元类默认参数值 (Delphi 2009),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31080978/

相关文章:

delphi - Delphi中如何获取64位随机值?

delphi - 如何在Delphi中在调整大小的TImage的整个区域上绘图?

Groovy 将代码添加到构造函数

python - 谁调用元类

python - 如何将实例成员的默认参数值传递给方法?

delphi - TServerSocket : Confusion with Socket Objects

delphi - 为什么 GetExtendedTcpTable 中的连接状态为 0

python - 元类和 __prepare__ ()

c++ - 参数列表中间的默认参数?

c++ - 为什么编译器不能从默认参数中推导出模板类型?