【问题标题】:own namespace for nested enums in DelphiDelphi中嵌套枚举的自己的命名空间
【发布时间】:2016-07-29 09:54:43
【问题描述】:

有没有办法将 Delphi 中的嵌套枚举放入自己的命名空间?

此代码生成 E2004:标识符重新声明,因为两个枚举都包含“未知”。

TMyType1 = class
public type
  TMyType1Enum = (unknown, val1, val2);
public
  constructor Create();
  ...
end;

TMyType2 = class
public type
  TMyType2Enum = (unknown, other1, other2, other3); // causes E2004
public
  constructor Create();
  ...
end;

在 C++ 中,枚举元素的标识符都在不同的范围内(TMyType1::unknown 和 TMyType2::unknown)。

除了前缀或后缀标识符(MyType1EnumUnknown,MyType1EnumVal1,...,MyType2Enumunknown,...)之外,是否有可能在 Delphi 中实现类似的功能?

【问题讨论】:

  • 下面的答案是完美的,但从编码标准的角度来看,前缀通常在 Delphi 中用于枚举。以TFontStyle 为例。它的定义是这样的:TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut)

标签: delphi enums delphi-xe2


【解决方案1】:

试试$SCOPEDENUMS。来自http://docwiki.embarcadero.com/RADStudio/en/Scoped_Enums_(Delphi)

type
  TFoo = (A, B, Foo);
  {$SCOPEDENUMS ON}
  TBar = (A, B, Bar);
  {$SCOPEDENUMS OFF}

begin
  WriteLn(Integer(Foo)); 
  WriteLn(Integer(A)); // TFoo.A
  WriteLn(Integer(TBar.B));
  WriteLn(Integer(TBar.Bar));
  WriteLn(Integer(Bar)); // Error
end;

【讨论】:

  • FWIW,现在 docwiki 似乎处于离线状态。但是可以在 Delphi/RAD Studio 附带的已安装帮助文件中找到相同的文档。我们无法从这里链接到它。
猜你喜欢
  • 2011-10-28
  • 2012-10-18
  • 2020-03-15
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多