Unable to understand Delphi XE6 protected types

I create two units and put the first class in one of them:

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. 

And I put the derived class in the second block:

 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. 

When I try to compile, I get

[dcc32 error] UDerClass.pas (22): E2362 Unable to access protected character TBaseOuterClass.TBaseInnerClass

in the line function TDerOuterClass.GetInnerInstance: TBaseOuterClass.TBaseInnerClass;

I do not understand why TBaseOuterClass.TBaseInnerClass (as an internal protected class) is not available from TDerOuterClass (which is obtained for TBaseOuterClass). What are the types actually protected in this case?

I did not find any explanation for this in the topic of declarations of nested types . So are there any reasons for this behavior?

This also applies to simple protected types, e.g.

 protected type TSimpleType = Integer; 

I can not write a function in TDerOuterClass

 protected function GetValue: TSimpleType; 

since i get the message

[dcc32 error] UDerClass.pas (16): E2003 Undeclared identifier: 'TSimpleType'

+7
delphi delphi-xe6
source share
1 answer

Sounds like a mistake. I suggest publishing it in a quality portal .

Now you can declare a type alias to trick the compiler (tested in 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. 
+12
source share

All Articles