无法理解Delphi XE6保护类型

前端之家收集整理的这篇文章主要介绍了无法理解Delphi XE6保护类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了两个单元并将第一个类放入其中一个:
unit UBaseClass;

  interface

  type
    TBaSEOuterClass = class
    protected type

      TBaseInnerClass = class
      public
      end;

    protected
      function GetInnerInstance: TBaSEOuterClass.TBaseInnerClass; virtual;
    end;

  implementation

  { TBaSEOuterClass }

  function TBaSEOuterClass.GetInnerInstance: TBaSEOuterClass.TBaseInnerClass;
  begin
  // doesn't matter
  end;

  end.

我将派生类放入第二个单元:

unit UDerClass;

interface

uses
  UBaseClass;

type
  TDerOuterClass = class(TBaSEOuterClass)
  protected type

    TDerInnerClass = class(TBaseInnerClass)

    end;

  protected
    function GetInnerInstance: TBaSEOuterClass.TBaseInnerClass; override;
  end;

implementation

{ TDerOuterClass }

function TDerOuterClass.GetInnerInstance: TBaSEOuterClass.TBaseInnerClass;
begin

end;

end.

当我想要编译时,我得到了

[dcc32 Error] UDerClass.pas(22): E2362 Cannot access protected symbol
TBaSEOuterClass.TBaseInnerClass

在行函数TDerOuterClass.GetInnerInstance:TBaSEOuterClass.TBaseInnerClass;

我无法理解为什么TBaSEOuterClass.TBaseInnerClass(作为内部受保护类)无法从TDerOuterClass(为TBaSEOuterClass派生)访问.在这种情况下,实际上受保护的类型是什么?

我在Nested Type Declarations topic没有找到任何解释.所以有这种行为的原因吗?

它也与简单的受保护类型相关

protected type    
  TSimpleType = Integer;

我无法在TDerOuterClass中编写函数

protected    
  function GetValue: TSimpleType;

因为我会收到一条消息

[dcc32 Error] UDerClass.pas(16): E2003 Undeclared identifier:
‘TSimpleType’

解决方法

它看起来像一个bug.我建议把它发布到 quality portal.

现在你可以声明类型别名来欺骗编译器(在XE7中测试).

unit UDerClass;

interface

uses
  UBaseClass;

type
  TDerOuterClass = class(TBaSEOuterClass)
  protected type
    TBaseInnerClass = TBaSEOuterClass.TBaseInnerClass; // <= type alias to avoid compiler error

    TDerInnerClass = class(TBaseInnerClass)

    end;

  protected
    function GetInnerInstance: TBaseInnerClass; override;
  end;

implementation

{ TDerOuterClass }

function TDerOuterClass.GetInnerInstance: TBaseInnerClass;
begin
  Result := TDerInnerClass.Create;
end;

end.
原文链接:https://www.f2er.com/delphi/101909.html

猜你在找的Delphi相关文章