What is the Delphi equivalent of a C++ template?
Posted on 2002-06-11
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:
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...