Solved

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

Posted on 2002-06-11
8
630 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 24

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

13 Experts available now in Live!

Get 1:1 Help Now