Search code examples
delphiinheritanceprotected

How to inherit from a class with a protected data type?


I have a base generics class with an inner protected class. How do I inherit from the base class and access the protected inner class?

As an example this code will not compile:

unit uFoo;

interface

type

  TFoo<T> = class
  protected
    type
      TFooProtected = class

      end;
  end;

  TFoo2<T> = class(TFoo<T>)
  protected
    item: TFooProtected;
  end;

Solution

  • Like this:

    type
      TFoo<T> = class
      protected
        type
          TFooProtected = class
          end;
      end;
    
      TFoo2<T> = class(TFoo<T>)
      protected
        item: TFoo<T>.TFooProtected;
      end;
    

    Note that this has nothing to do with generics. It is valid for any class where the type is declared internally.