Board index » delphi » Q: C++ class template possible?

Q: C++ class template possible?

In C++ there is the possibility to add a data template to a class
declaration. With this construct the datatype of some variables can stay
undeclared until a derived class is declared. In the declaration of
several derived classes the datatype of the template can be assigned to
different types.

Does anyone know whether it is possible to create a similar thing in
Delphi?

Thanks in advance,

Frens Vonken

===================================================================
J.F. Vonken                                
Dutch Technical Teachers' Training College
P.O. Box 826                              
The Netherlands    
Telephone:+31402474082 Fax: +3140244396  Email: J.F.Von...@pth.nl
====================================================================

 

Re:Q: C++ class template possible?


Quote
Frens Vonken <J.F.Von...@pth.nl> wrote:
>In C++ there is the possibility to add a data template to a class
>declaration. With this construct the datatype of some variables can stay
>undeclared until a derived class is declared. In the declaration of
>several derived classes the datatype of the template can be assigned to
>different types.
>Does anyone know whether it is possible to create a similar thing in
>Delphi?
>Thanks in advance,
>Frens Vonken

I think the answer is no.  Don't I wish it was yes.  Delphi doesn't
even have macro substitution for it's $DEFINE directive, sigh.  As
long at I'm at it I'm going to lament the lack of multiple inheritence
too.  Sigh.

Delphi 3.0 - based on C++?  please oh please...
--
Tim Shea
CSI
c...@citysoft.com

Re:Q: C++ class template possible?


Quote
>In C++ there is the possibility to add a data template to a class
>declaration. With this construct the datatype of some variables can stay
>undeclared until a derived class is declared. In the declaration of
>several derived classes the datatype of the template can be assigned to
>different types.

     Depending on what specifically the situation you need is, you can use a
type of the "highest common ancestor".
_
Pennsylvania: Communistwealth of Pennsylvania

+------------------------------------------------------------+
|Chad Z. Hower  -  phoe...@pobox.com                         |
|Phoenix Business Enterprises - p...@pobox.com  - www.pbe.com |
|Physically in Church Hill, TN - Logically Not Sure          |
+------------------------------------------------------------+

Quote
>>SQUID - The ultimate database reader, and NO limits. #$737961

**Special Compile: 1.033B (Beta)

Re:Q: C++ class template possible?


Quote
Frens Vonken <J.F.Von...@pth.nl> wrote:
>In C++ there is the possibility to add a data template to a class
>declaration. With this construct the datatype of some variables can stay
>undeclared until a derived class is declared. In the declaration of
>several derived classes the datatype of the template can be assigned to
>different types.
>Does anyone know whether it is possible to create a similar thing in
>Delphi?

There is no compiler support for templates, but you can use class references
(aka metaclasses) to approximate their use.  The following example shows a
"component generator" that takes a component class reference as a constructor
parameter, and will generate components of that component class:

----
interface

[...]

  TComponentGenerator = class(TObject)
  private
    FCompClass: TComponentClass;
  public
    constructor Create(ACompClass: TComponentClass);
    function Generate(AOwner: TComponent): TComponent;
  end;

implementation

[...]

constructor TComponentGenerator.Create(ACompClass: TComponentClass);
begin
  inherited Create;
  FCompClass := ACompClass;
end;

function TComponentGenerator.Generate(AOwner: TComponent): TComponent;
begin
  { note that this works because TComponent's constructor is virtual }
  Result := FCompClass.Create(AOwner);
end;

------

You could then use this class like the following:

procedure TForm1.Button1Click(Sender: TObject);
var
  comp: TComponent;
  compgen: TComponentGenerator;
begin

  { we want to generate buttons, so pass the button class to the Component
    Generator constructor }
  compgen := TComponentGenerator.Create(TButton);

  { now generate a button }
  comp := compgen.Generate(self);

  { set button properties }
  with comp as TButton do
  begin
    Parent := self;
    Caption := 'A Generated Button!';
  end;

  compgen.Free;

end;

------

With similar techniques, you can create container classes.  Let me know if you
have any questions...

--------------------------------------------------
brent_byso...@mindlink.bc.ca
VB, Delphi & SQL Development
Vancouver, BC.
(604) 689-2616
-------------------------------------------------

Re:Q: C++ class template possible?


Quote
Brent Bysouth wrote:

>> Does anyone know whether it is possible to create [templates] in
>> Delphi?
> [... elided ... ]
>   TComponentGenerator = class(TObject)
>   private
>     FCompClass: TComponentClass;
>   public
>     constructor Create(ACompClass: TComponentClass);
>     function Generate(AOwner: TComponent): TComponent;
>   end;
> [... elided ...]

> You could then use this class like the following:

> procedure TForm1.Button1Click(Sender: TObject);
> var
>   comp: TComponent;
>   compgen: TComponentGenerator;
> begin

>   { we want to generate buttons, so pass the button class to the Component
>     Generator constructor }
>   compgen := TComponentGenerator.Create(TButton);
> [... elided ... ]

Haven't used Delphi, but aren't you passing a TYPE to a normal pass-by-value
parameter here?  At least, it looks very suspicious:  how can the compiler
differentiate between normal parameters and type parameters when there is no
difference in the declaration syntax?

It's true that you can implement, say, a generic linked list using only
subclassing.  But using *only* subclassing (not a true template mechanism)
you have to downcast what you take out of the list.  So you don't get
strict type checking this way, which is what templates are all about.  Does
Delphi address this question?

Re:Q: C++ class template possible?


"Alf P. Steinbach" <al...@telepost.no> wrote:

[...]

Quote
>>   TComponentGenerator = class(TObject)
>>   private
>>     FCompClass: TComponentClass;
>>   public
>>     constructor Create(ACompClass: TComponentClass);
>>     function Generate(AOwner: TComponent): TComponent;
>>   end;
>> [... elided ...]

>> You could then use this class like the following:

>> procedure TForm1.Button1Click(Sender: TObject);
>> var
>>   comp: TComponent;
>>   compgen: TComponentGenerator;
>> begin

>>   { we want to generate buttons, so pass the button class to the Component
>>     Generator constructor }
>>   compgen := TComponentGenerator.Create(TButton);
>> [... elided ... ]
>Haven't used Delphi, but aren't you passing a TYPE to a normal pass-by-value
>parameter here?  At least, it looks very suspicious:  how can the compiler
>differentiate between normal parameters and type parameters when there is no
>difference in the declaration syntax?

Delphi introduces a class reference type that refers to classes:  as classes
describe objects, class references describe classes.  You create a class
reference by using the "class of" directive -- in the above example,
TComponentClass happens to be declared as:

    TComponentClass = class of TComponent;

This allows you to pass around class types as if they were variables.  The
following constructor takes a class reference type of TComponentClass:

    constructor Create(ACompClass: TComponentClass);

When you invoke this constructor, you can pass the TComponent class or any of
its derivatives (TButton, TListBox, whatever!)

Quote
>It's true that you can implement, say, a generic linked list using only
>subclassing.  But using *only* subclassing (not a true template mechanism)
>you have to downcast what you take out of the list.  So you don't get
>strict type checking this way, which is what templates are all about.  Does
>Delphi address this question?

You can make container classes by using class references and doing _runtime_
type checking via the 'is' RTTI operator.

--------------------------------------------------
brent_byso...@mindlink.bc.ca
VB, Delphi & SQL Development
Vancouver, BC.
(604) 689-2616
-------------------------------------------------

Other Threads