⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3sharing.html

📁 Visual C++ has been one of most effective tool for the large industrial applications. This book is t
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    <td class=codeTable>
<pre>template &lt;class T&gt;</pre>
    </td></tr>
</table>
<!-- End Code -->

and replace <var>int</var>s with <var>T</var>s. You are parametrizing the definition of <var>List</var> with the parameter <var>T</var> that stands for any type. Once you have the template defined, you can instantiate it by substituting any type for <var>T</var>--it doesn't even have to be a class. For instance,

<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>List&lt;int&gt; myList;</pre>
    </td></tr>
</table>
<!-- End Code -->

will define a <var>List</var> of integers called <var>myList</var>. But you are also free to declare other lists, such as a list of unsigned longs, <var>List&lt;unsigned long&gt;</var>, a list of doubles, <var>List&lt;double&gt;</var> or a list of <var>Star</var>s, <var>List&lt;Star&gt;</var>. 

<P>Here's the definition of the <var>List</var> template.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre><b>template&lt;class T&gt;</b>
class List
{
public:
    List ();
    ~List ();
    void Add (<b>T</b> value);
private:
    class Link
    {
    public:
        Link (Link * pNext, <b>T</b> value)
            : _pNext (pNext), _value (value) {}

        Link *  Next () const { return _pNext; }
        <b>T</b>       GetValue () const { return _value; }
    private:
        Link *  _pNext;
        <b>T</b>       _value;
    };

public:
    class Seq
    {
    public:
        Seq (List const &amp; list)
            : _pLink (list.GetHead ()) {}
        bool AtEnd () const { return _pLink == 0; }
        void Advance () { _pLink = _pLink-&gt;Next (); }
        <b>T</b> GetValue () const { return _pLink-gt;GetValue (); }
    private:

        Link const * _pLink; // current link
    };

    friend Seq;
private:
    Link const * GetHead () const { return _pHead; }

    Link * _pHead;
};
</pre>
    </td></tr>
</table>
<!-- End Code -->
<P>To be more general, I've changed the name of the method that retrieves the stored value to <var>GetValue</var>.

<P>For technical reasons, today's compilers require all template code to be visible in the place of template instantiation (this requirement might change in future compilers). What that means is that we have to put all the method implementations in the same header file as the template class definition. It doesn't make these methods inline (unless they are declared as such) nor does it imply that their code will be instantiated multiple times (unless you have a very cheap compiler).

<P>Here's how you define the constructor--notice that only the first <var>List</var>, the one before the double colon, is followed by &lt;T&gt;. 
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre><b>template &lt;class T&gt;</b>
List<b>&lt;T&gt;</b>::List ()
    : _pHead (0)
{}</pre>
    </td></tr>
</table>
<!-- End Code -->

<P>Same with the destructor:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre><b>template &lt;class T&gt;</b>
List<b>&lt;T&gt;</b>::~List ()
{
    // free linked list
    while (_pHead != 0)
    {
        Link * pLink = _pHead;
        _pHead = _pHead-&gt;Next ();
        delete pLink;
    }
}</pre>
    </td></tr>
</table>
<!-- End Code -->

<P>Other methods follow the same pattern.

<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre><b>template &lt;class T&gt;</b>
void List<b>&lt;T&gt;</b>::Add (<b>T</b> val)
{
    Link * pLink = new Link (_pHead, val);
    _pHead = pLink;
}</pre>
    </td></tr>
</table>
<!-- End Code -->

<P>The second part of "templatization" is to substitute all the uses of <var>List</var> of integers with <var>List&lt;int&gt;</var>

<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>class HTable
{
public:
    explicit HTable (int size): _size(size)
    {
        _aList = new List<b>&lt;int&gt;</b> [size];
    }

    ~HTable ()
    {
        delete []_aList;
    }

    void Add (char const * str, int id);
public:
    class Seq: public List<b>&lt;int&gt;</b>::Seq
    {
    public:
        Seq (HTable const &amp; htab, char const * str)
            : List<b>&lt;int&gt;</b>::Seq (htab.Find (str)) {}
    };

    friend Seq;
private:
    List<b>&lt;int&gt;</b> const &amp; Find (char const * str) const;
    int hash (char const * str) const;

    List<b>&lt;int&gt;</b> * _aList;
    int            _size;
};</pre>
    </td></tr>
</table>
<!-- End Code -->

<p class=summary>To summarize: A class template is a definitions of a class that is parametrized by one or more types. The type parameters are listed within angle brackets after the keyword template. 
<p class=summary>template &lt;class type1, class type2 ...&gt;

<p class=summary>To instantiate the template it is enough to use it, with the type parameters substituted by actual types (either built-in or user-defined). 

    </td>
    </tr>
</table>
<!-- End Main Table -->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -