Nested types

Top  Previous  Next

New features since Delphi 7 > Generics > Nested types

 

A nested type within a generic is itself a generic.

 

type

  TFoo<T> = class

  type

    TBar = class

      X: Integer;

      // ...

    end;

  end;

 

  // ...

  TBaz = class

  type

    TQux<T> = class

      X: Integer;

      // ...

    end;

    // ...

  end;

 

var

   n: TFoo<Double>.TBar;

 

->

 

//---------------------------------------------------------------------------

template<typename T>

class TFoo : public System::TObject

{

  typedef System::TObject inherited;

  friend class TBaz;

public:

      // ...

//---------------------------------------------------------------------------

  class TBar : public System::TObject

  {

    typedef System::TObject inherited;

  public:

    int X;

    void InitMembers(){X = 0;}

public:

    TBar() {InitMembers();}

  };

 

public:

  TFoo() {}

};

 

 

  // ...

//---------------------------------------------------------------------------

class TBaz : public System::TObject

{

  typedef System::TObject inherited;

  //#   template<typename T> friend class TFoo;

public:

      // ...

//---------------------------------------------------------------------------

  template<typename T>

  class TQux : public System::TObject

  {

    typedef System::TObject inherited;

  public:

    int X;

    void InitMembers(){X = 0;}

public:

    TQux() {InitMembers();}

  };

 

    // ...

public:

  TBaz() {}

};

 

extern TFoo<double>::TBar* n;

 

 

A generic can also be declared within a regular class as a nested type:

 

type

   TOuter = class

   type

     TData<T> = class

       FFoo1: TFoo<Integer>;         // declared with closed constructed type

       FFoo2: TFoo<T>;               // declared with open constructed type

       FFooBar1: TFoo<Integer>.TBar; // declared with closed constructed type

       FFooBar2: TFoo<T>.TBar;       // declared with open constructed type

       FBazQux1: TBaz.TQux<Integer>; // declared with closed constructed type

       FBazQux2: TBaz.TQux<T>;       // declared with open constructed type

       //...

     end;

   var

     FIntegerData: TData<Integer>;

     FStringData: TData<String>;

   end;

 

->

 

//---------------------------------------------------------------------------

class TOuter : public System::TObject

{

  typedef System::TObject inherited;

public:

//---------------------------------------------------------------------------

  template<typename T>

  class TData : public System::TObject

  {

    typedef System::TObject inherited;

  public:

    TFoo<int>* FFoo1;

    TFoo<T>* FFoo2;

    TFoo<int>::TBar* FFooBar1;

// doesn't compile: TFoo<T>::TBar* FFooBar2;

    TBaz::TQux<int>* FBazQux1;

    TBaz::TQux<T>* FBazQux2;

       //...

public:

    TData() {}

  };

  TData<int>* FIntegerData;

  TData<System::String>* FStringData;

public:

  TOuter() {}

 };

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



This page belongs to the DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content