📄 list.htm
字号:
TList<b>:</b><b>:</b>Put<b>(</b>Index<b>,</b>Item<b>)</b><b>;</b>
<b>}</b>
<b>public</b><b>:</b>
<b>__fastcall</b> TTypedList<b>(</b><b>bool</b> bFreeObjects <b>=</b> <b>false</b><b>)</b>
<b>:</b>TList<b>(</b><b>)</b><b>,</b>
bAutoDelete<b>(</b>bFreeObjects<b>)</b>
<b>{</b>
<b>}</b>
<font color="navy">// Note: No destructor needed. TList::Destroy calls Clear,</font>
<font color="navy">// and Clear is virtual, so our Clear runs.</font>
<b>int</b> <b>__fastcall</b> Add<b>(</b>T<b>*</b> Item<b>)</b>
<b>{</b>
<b>return</b> TList<b>:</b><b>:</b>Add<b>(</b>Item<b>)</b><b>;</b>
<b>}</b>
<b>void</b> <b>__fastcall</b> Delete<b>(</b><b>int</b> Index<b>)</b>
<b>{</b>
<b>if</b><b>(</b>bAutoDelete<b>)</b>
<b>delete</b> Get<b>(</b>Index<b>)</b><b>;</b>
TList<b>:</b><b>:</b>Delete<b>(</b>Index<b>)</b><b>;</b>
<b>}</b>
<b>void</b> <b>__fastcall</b> Clear<b>(</b><b>void</b><b>)</b>
<b>{</b>
<b>if</b><b>(</b>bAutoDelete<b>)</b>
<b>{</b>
<b>for</b> <b>(</b><b>int</b> j<b>=</b><font color="blue">0</font><b>;</b> j<Count<b>;</b> j<b>++</b><b>)</b>
<b>delete</b> Items<b>[</b>j<b>]</b><b>;</b>
<b>}</b>
TList<b>:</b><b>:</b>Clear<b>(</b><b>)</b><b>;</b>
<b>}</b>
T<b>*</b> <b>__fastcall</b> First<b>(</b><b>void</b><b>)</b>
<b>{</b>
<b>return</b> <b>(</b>T<b>*</b><b>)</b>TList<b>:</b><b>:</b>First<b>(</b><b>)</b><b>;</b>
<b>}</b>
<b>int</b> <b>__fastcall</b> IndexOf<b>(</b>T<b>*</b> Item<b>)</b>
<b>{</b>
<b>return</b> TList<b>:</b><b>:</b>IndexOf<b>(</b>Item<b>)</b><b>;</b>
<b>}</b>
<b>void</b> <b>__fastcall</b> Insert<b>(</b><b>int</b> Index<b>,</b> T<b>*</b> Item<b>)</b>
<b>{</b>
TList<b>:</b><b>:</b>Insert<b>(</b>Index<b>,</b>Item<b>)</b><b>;</b>
<b>}</b>
T<b>*</b> <b>__fastcall</b> Last<b>(</b><b>void</b><b>)</b>
<b>{</b>
<b>return</b> <b>(</b>T<b>*</b><b>)</b> TList<b>:</b><b>:</b>Last<b>(</b><b>)</b><b>;</b>
<b>}</b>
<b>int</b> <b>__fastcall</b> Remove<b>(</b>T<b>*</b> Item<b>)</b>
<b>{</b>
<b>int</b> nIndex <b>=</b> TList<b>:</b><b>:</b>Remove<b>(</b>Item<b>)</b><b>;</b>
<font color="navy">// Should I delete a pointer that is being passed to me.</font>
<font color="navy">// If bAutoDelete is true, then assume that we are always</font>
<font color="navy">// responsible for deleting a pointer that is added to the</font>
<font color="navy">// list. If the item was found, then delete the pointer.</font>
<b>if</b><b>(</b>bAutoDelete <b>&&</b> <b>(</b>nIndex <b>!=</b> <b>-</b><font color="blue">1</font><b>)</b><b>)</b>
<b>delete</b> Item<b>;</b>
<b>return</b> nIndex<b>;</b>
<b>}</b>
<b>__property</b> T<b>*</b> Items<b>[</b><b>int</b> Index<b>]</b> <b>=</b> <b>{</b>read<b>=</b>Get<b>,</b> write<b>=</b>Put<b>}</b><b>;</b>
<b>}</b><b>;</b>
<font color="green">#endif</font>
</pre>
<BR>
<H3>
<A NAME="code">Code examples</A>
</H3>
<pre>
<font color="navy">//----------------------------------------------------------------------------</font>
<font color="navy">// Code Example 1: A list of TButton's</font>
<font color="green">#incude "typedlist.h"</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>CreateButtons<b>(</b><b>)</b>
<b>{</b>
<font color="navy">// Create a list of buttons. Do not auto delete, form will delete</font>
TTypedList <TButton<b>></b> <b>*</b>ButtonList <b>=</b> <b>new</b> TTypedList <TButton<b>></b><b>(</b><b>false</b><b>)</b><b>;</b>
ButtonList<b>-></b>Add<b>(</b><b>new</b> TButton<b>(</b><b>this</b><b>)</b><b>)</b><b>;</b>
ButtonList<b>-></b>Add<b>(</b><b>new</b> TButton<b>(</b><b>this</b><b>)</b><b>)</b><b>;</b>
ButtonList<b>-></b>Add<b>(</b><b>new</b> TButton<b>(</b><b>this</b><b>)</b><b>)</b><b>;</b>
<font color="navy">// ButtonList->Add(Application); <<-- does not compile</font>
<b>for</b> <b>(</b><b>int</b> j<b>=</b><font color="blue">0</font><b>;</b> j<ButtonList<b>-></b>Count<b>;</b> j<b>++</b><b>)</b>
<b>{</b>
ButtonList<b>-></b>Items<b>[</b>j<b>]</b><b>-></b>Caption <b>=</b> <font color="blue">"Button"</font> <b>+</b> IntToStr<b>(</b>j<b>)</b><b>;</b>
ButtonList<b>-></b>Items<b>[</b>j<b>]</b><b>-></b>Left <b>=</b> <font color="blue">250</font><b>;</b>
ButtonList<b>-></b>Items<b>[</b>j<b>]</b><b>-></b>Top <b>=</b> <font color="blue">50</font> <b>+</b> j<b>*</b><font color="blue">25</font><b>;</b>
ButtonList<b>-></b>Items<b>[</b>j<b>]</b><b>-></b>Parent <b>=</b> <b>this</b><b>;</b>
<b>}</b>
<b>delete</b> ButtonList<b>;</b>
<b>}</b>
<font color="navy">//----------------------------------------------------------------------------</font>
<font color="navy">// Code Example 2: Cleaning up template syntax</font>
<font color="green">#incude "typedlist.h"</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>CreateButtons<b>(</b><b>)</b>
<b>{</b>
<b>typedef</b> TTypedList <TButton<b>></b> TButtonList<b>;</b>
TButtonList <b>*</b>ButtonList <b>=</b> <b>new</b> TButtonList<b>(</b><b>true</b><b>)</b><b>;</b>
ButtonList<b>-></b>Add<b>(</b><b>new</b> TButton<b>(</b><b>this</b><b>)</b><b>)</b><b>;</b>
<b>...</b>
<b>delete</b> ButtonList<b>;</b>
<b>}</b>
<font color="navy">//----------------------------------------------------------------------------</font>
<font color="navy">// Code Example 3: A list of tables and queries</font>
<font color="green">#incude "typedlist.h"</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>OpenDataSets<b>(</b><b>)</b>
<b>{</b>
<b>typedef</b> TTypedList <TDataSet<b>></b> TDataSetList<b>;</b>
TDataSetList <b>*</b>list <b>=</b> <b>new</b> TDataSetList<b>(</b><b>false</b><b>)</b><b>;</b>
list<b>-></b>Add<b>(</b>Table1<b>)</b><b>;</b>
list<b>-></b>Add<b>(</b>Table2<b>)</b><b>;</b>
list<b>-></b>Add<b>(</b>Table3<b>)</b><b>;</b>
list<b>-></b>Add<b>(</b>Query1<b>)</b><b>;</b>
<b>for</b> <b>(</b><b>int</b> j<b>=</b><font color="blue">0</font><b>;</b> j<list<b>-></b>Count<b>;</b> j<b>++</b><b>)</b>
list<b>-></b>Items<b>[</b>j<b>]</b><b>-></b>Active <b>=</b> <b>true</b><b>;</b>
<b>delete</b> list<b>;</b>
<b>}</b>
</pre>
<BR>
<H3>
<A NAME="notes">Notes:</A>
</H3>
<P>
<B>No need to cast:</B> Notice that the code examples above do not contain any casting. Casting is unecessary because
<TT>TTypedList</TT> provides a type safe <TT>Items</TT> property. The read method for the <TT>Items</TT> array property
is the <TT>Get</TT> function. Notice that <TT>Get</TT> returns a pointer to a template T object instead of returning a
void pointer. When you create a list of <TT>TButton</TT> pointers, <TT>Get</TT> returns a <TT>TButton</TT> pointer.
When you create a list of <TT>TDataSet</TT> pointers, <TT>Get</TT> returns a <TT>TDataSet</TT> pointer. The
<TT>Get</TT> function returns the type of object that you need, which makes casting unecessary.
</P>
<P>
<B>Compile time safety:</B> <TT>TTypedList</TT> contains an <TT>Add</TT> function that replaces the <TT>Add</TT>
function of the base class. The <TT>Add</TT> function takes the template type as its argument, instead of taking a
void pointer. When you create a list of <TT>TButton</TT> pointers, the compiler only allows you to pass
<TT>TButton</TT> pointers to the <TT>Add</TT> function. If you attempt to pass anything else, the compiler will
generate an error.
</P>
<P>
<B>Little Overhead:</B> Because the member functions of <TT>TTypedList</TT> are defined inline, the class incurs no
additional overhead. The compiler enforces type safety when you call a method of <TT>TTypedList</TT>. Once the compiler
is satisfied that you have passed the correct type of object, it inserts code that calls the methods of <TT>TList</TT>
directly (you can prove this by viewing the assembly code that is generated for a call to the <TT>Add</TT>
method of <TT>TTypedList</TT>). <TT>TTypedList</TT> provides compile time safety without sacrificing runtime
performance.
</P>
<P>
<B>Consider using STL templates instead:</B> <TT>TTypedList</TT> is awesome. Awesome, that is, in comparison to what it
replaces; the relatively wimpy and unsafe <TT>TList</TT>. That said, I encourage you not to overuse the class. Consider
using STL containers whenever possible. STL containers are more portable and more flexible than <TT>TTypedList</TT>,
plus they provide type safety that is just as good, if not superior to, that of <TT>TTypedList</TT>. A good STL replacement
for <TT>TList</TT> is the <TT>vector</TT> template.
</P>
<P>
<B>Altering the way <TT>TTypedList</TT> deletes items:</B> Notice that the <TT>Remove</TT> and <TT>Put</TT> methods
will delete an item if the auto delete flag is set. You may not want the class to behave this way. Perhaps you only
want the list to delete an object when the list itself is deleted. I have taken the approach that if auto delete is on,
then the list assumes ownership of the pointer, and the list will be responsible for deleting that pointer. You may want
to alter this behavior, especially in the <TT>Put</TT> method.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -