Solved

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

Posted on 2002-06-11
8
631 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now