Solved

What is the Delphi equivalent of a C++ template?

Posted on 2002-06-11
8
632 Views
Last Modified: 2008-02-01
I'm trying to create list classes based on the TList class (e.g. TIntegerList, TRealList, TMyRecordList, etc.), and I'm looking for an elegant way to do so without copying the source code over and over again with the necessary modifications.

I thought of using something like the templates of C++, but I'm not familiar with its Delphi counterpart.

To be more specific, I modified the TList class that instead of being a list of pointers, it will be a list of TItem - which is a type I defined as follows:

type
  TItem = Integer;

and thus created TItemList, which is a list of integers. Changing TItem's definition to TItem = Real for example, will create a list of real numerical values, etc. The problem is that this definition is too static, and if i need two different list classes, i would have to copy the source code twice, change the classes' names, and redifine each of the classes' TItem type. In C++, I can use an element called a Template to leave the type of TItem "open", and any class that inherites from TItemList can determines the type of TItem, thus creating a list of whatever type of elements i desire.

I'm looking for a way to do the same in Delphi. or perhaps a better suggestion to implement several list classes, each with a different type of list items...
0
Comment
Question by:QwertyNickNacker
8 Comments
 
LVL 3

Expert Comment

by:raidos
ID: 7070076
Not sure what C++ templates are, but inheriting from other classes is the way to do it if you ask me..


Type
  TMyTemplateClass = Class(TList)
    Protected
    Private
    Public
      Constructor Create;
      Destructor Destroy;
      Procedure MySpecialProc;
  End;
 
  TMyIntegerList = Class(TMyTemplateClass)
    Protected
    Private
    Public
  End;    

Implementation
 
Constructor TMyTemplateClass.Create;
Begin
  Inherited;
  //Special inits down below;
End;

Destructor TMyTemplateClass.Destroy;
Begin
  //Destroy stuff that needs to be destroyed
  Inherited;
End;

Procedure TMyTemplateClass.MySpecialProc;
Begin

End;

Hope you get the general idea.
Regards
//raidos
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7071156
adding, getting, setting the item is also easy

you can check out my TlnDynBuffer or TSections at :
http://lee.nover.has.his.evilside.org/isapi/pas2html.dll/pas2html?File=/delphi/MiscFiles/vn_common/lnVidTypes.pas



0
 
LVL 6

Expert Comment

by:swift99
ID: 7074217
C++ templates were an attempt to achieve through inheritance what modern OO languages do through interfaces.

In delphi you can inherit from a class and/or you can inherit an interface.  This is more like Java than C++.

type
    // for full equivalence to the C++ templating
    IMyStuff = interface (IUnknown)
        methods
        properties
    end;

    // Normally this is all you will need
    myList = class (Tlist)
        methods
        properties
    end;

    // Obviously you can have descendents of many classes that also inherit the interface
    myInterfacedList = class (TList, iMyStuff)
        methods
        properties
    end;

    myInterfacedControl = class (TControl, IMyStuff)
        methods
        properties
    end;

Anything declared in the interface must be implemented in the class.  
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Expert Comment

by:Omycron
ID: 7074951
I think C++ templates have been created to give programmers the possibility to write datatype independent sourcecode. Templates are mainly used to implement algorithms, I do not know where the inheritance is hidden ;-) To my mind C++ is a very modern OO language, Delphi is copying C++ more and more.

regards
Omycron
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 7075211
Hi!
Yes, Omycron is right, the purpose of C++ templates is to give the possibility to write datatype independent sourcecode.
To use C++ template A with class B the methods and operators of A must be declared in B to take full advantage of A.
And yes, it is similar to the use of Interfaces. But I have found out that interfaces are not as flexible as templates.

Regards,
  Tomas Helgi
0
 
LVL 6

Expert Comment

by:swift99
ID: 7083671
Fair enough.  My understanding was that Java pioneered the "interface" (which was later mimicked by Delphi) because of problems inherent in the C++ template approach.  In retrospect, I believe the "problems" were more of design philosophy than technical nature.  This is in line with TomasHelgi's comment that interfaces are less flexible.  They certainly create a different design paradigm.

In that regard I would say that since there is no compiler string substitution facilities for the application code in Delphi/Pascal, there is no direct equivalent to the template.
0
 
LVL 4

Accepted Solution

by:
SurferJoe earned 400 total points
ID: 7089948
Not sure exactly what you’re trying to accomplish, I found creating classes or components real bug prone, unless that s all you have to work on.

Why not use arrays to store you list(s)?

Or even a variant array, I love variants!
Something like this;

procedure TForm1.Button1Click(Sender: TObject);
var
 myArray: Variant;
 x, y: integer;

begin
   x := xWhatEver;
   y := yWhatEver;

   myArray := VarArrayCreate([1, x, 1, y], varVariant);

   for x := 1 to xWhatEver do
    for y := 1 to yWhatEver do
      myArray[ x, y] := (most anything you could want);


end;
0
 

Author Comment

by:QwertyNickNacker
ID: 7184962
Hey,
 
  I didn't find the equivalent of C++ templates, but your solution the best one, and the one the one i eventually used, so keep on the good work dude,
                                 
                                QwertyNickNacker.

P.S.
  Variants rule.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question