Base types

Top  Previous  Next

New features since Delphi 7 > Generics > Base types

 

 

The base type of a parameterized class or interface type might be an actual type or a constructed type

 

 

 type

    TFoo1<T> = class(TBar)            // Actual type

    end;

 

    TFoo2<T> =  class(TBar2<T>)       // Open constructed type

    end;

 

    TFoo3<T> = class(TBar3<Integer>)  // Closed constructed type

    end;

 

->

 

            // Actual type

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

template<typename T>

class TFoo1 : public TBar

{

  typedef TBar inherited;

 

};

       // Open constructed type

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

template<typename T>

class TFoo2 : public TBar2<T>

{

  typedef TBar2<T> inherited;

 

};

  // Closed constructed type

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

template<typename T>

class TFoo3 : public TBar3<int>

{

  typedef TBar3<int> inherited;

 

};

 

 

Class, interface, record, and array types can be declared with type parameters.

 

type

   TRecord<T> = record

     FData: T;

   end;

 

 type

   IAncestor<T> = interface

     function GetRecord: TRecord<T>;

   end;

 

   IFoo<T> = interface(IAncestor<T>)

     procedure AMethod(Param: T);

   end;

 

 type

   TFoo<T> = class(TObject, IFoo<T>)

     FField: TRecord<T>;

     procedure AMethod(Param: T);

     function GetRecord: TRecord<T>;

   end;

 

 type

   anArray<T>= array of T;

   intArray= anArray<Integer>;

 

->

 

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

template<typename T>

struct TRecord

{

  T FData;

};

template<typename T>

class IAncestor

{

  public:

  virtual ~IAncestor() {}

  virtual TRecord<T> GetRecord() = 0;

};

template<typename T>

class IFoo : public IAncestor<T>

{

  public:

  virtual ~IFoo() {}

  virtual void AMethod(T Param) = 0;

};

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

template<typename T>

class TFoo : public System::TObject, IFoo<T>

{

  typedef System::TObject inherited;

public:

  TRecord<T> FField;

  void AMethod(T Param);

  TRecord<T> GetRecord();

public:

  TFoo() {}

};

template<typename T> using anArray = std::vector<T>;

typedef anArray<int> intArray;

 

 

 

 

 

 

 

 



This page belongs to the DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content