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

📄 ch26.htm

📁 VC使用所有细节的逻列
💻 HTM
📖 第 1 页 / 共 5 页
字号:
local variable. This illustrates how you can use the parameter types just as you would use any specific data type such as <font color="#008000">int</font> or <font color="#008000">char</font>.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>Because function templates are so flexible, they can often lead to trouble. For example, in the <font color="#008000">Min()</font> template, you have to be sure that the data types you supply as parameters can be compared. If you tried to compare two 
classes, your program would not compile unless the classes overloaded the <font color="#008000">&lt;</font> and <font color="#008000">&gt;</font> operators.</P>

<P>Another way you can run into trouble is when the arguments you supply to the template are not used as you think. For example, what if you added the following line to <font color="#008000">main()</font> in Listing 26.6?</P>

<pre><font color="#008000">cout &lt;&lt; Min(&quot;APPLE&quot;, &quot;ORANGE&quot;) &lt;&lt; endl;</font></pre>

<P>If you don't think about what you're doing in the previous line, you may jump to the conclusion that the returned result will be <font color="#008000">APPLE</font>. The truth is, however, that the preceding line may or may not give you the result you 
expect. Why? Because the <font color="#008000">&quot;APPLE&quot;</font> and <font color="#008000">&quot;ORANGE&quot;</font> string constants result in pointers to <font color="#008000">char</font>. This means that the program will compile fine, with the 
compiler creating a version of <font color="#008000">Min()</font> that compares <font color="#008000">char</font> pointers. But there's a big difference between comparing two pointers and comparing the data to which the pointers point. If <font 
color="#008000">&quot;ORANGE&quot;</font> happens to be stored at a lower address than <font color="#008000">&quot;APPLE&quot;</font>, the preceding call to <font color="#008000">Min()</font> results in <font color="#008000">&quot;ORANGE&quot;</font>.</P>

<P>A way to avoid this problem is to provide a specific replacement function for <font color="#008000">Min()</font> that defines exactly how you want the two string constants compared. When you provide a specific function, the compiler uses that function 
instead of creating one from the template. Listing 26.9 is a short program that demonstrates this important technique. When the program needs to compare the two strings, it doesn't call a function created from the template but instead uses the specific 
replacement function.</P>

<P><I>Listing 26.9&#151;TEMPLATE2.CPP&#151;Using a Specific Replacement Function</I></P>

<pre><font color="#008000">#include &lt;iostream.h&gt;</font></pre>

<pre><font color="#008000">#include &lt;string.h&gt;</font></pre>

<pre><font color="#008000">template&lt;class Type&gt;</font></pre>

<pre><font color="#008000">Type Min(Type arg1, Type arg2)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    Type min;</font></pre>

<pre><font color="#008000">    if (arg1 &lt; arg2)</font></pre>

<pre><font color="#008000">        min = arg1;</font></pre>

<pre><font color="#008000">    else</font></pre>

<pre><font color="#008000">        min = arg2;</font></pre>

<pre><font color="#008000">    return min;</font></pre>

<pre><font color="#008000">}</font></pre>

<pre><font color="#008000">char* Min(char* arg1, char* arg2)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    char* min;</font></pre>

<pre><font color="#008000">    int result = strcmp(arg1, arg2);</font></pre>

<pre><font color="#008000">    if (result &lt; 0)</font></pre>

<pre><font color="#008000">        min = arg1;</font></pre>

<pre><font color="#008000">    else</font></pre>

<pre><font color="#008000">        min = arg2;</font></pre>

<pre><font color="#008000">    return min;</font></pre>

<pre><font color="#008000">}</font></pre>

<pre><font color="#008000">int main()</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    cout &lt;&lt; Min(15, 25) &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; Min(254.78, 12.983) &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; Min('A', 'Z') &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; Min(&quot;APPLE&quot;, &quot;ORANGE&quot;) &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    return 0;</font></pre>

<pre><font color="#008000">}</font></pre>

<P><B>Creating Class Templates</B></P>

<P>Just as you can create abstract functions with function templates, so too can you create abstract classes with class templates. A class template represents a class, which in turn represents an object. When you define a class template, the compiler 
takes the template and creates a class. You then declare (instantiate) objects of the class. As you can see, class templates add another layer of abstraction to the concept of classes.</P>

<P>You define a class template much as you define a function template, by supplying the <font color="#008000">template</font> line followed by the class's declaration, as shown in Listing 26.10. Notice that, just as with a function template, you use the 
abstract data types given as parameters in the <font color="#008000">template</font> line in the body of the class in order to define member variables, return types, and other data objects.</P>

<P><I>Listing 26.10&#151; Defining a Class Template</I></P>

<pre><font color="#008000">template&lt;class Type&gt;</font></pre>

<pre><font color="#008000">class CMyClass</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">protected:</font></pre>

<pre><font color="#008000">    Type data;</font></pre>

<pre><font color="#008000">public:</font></pre>

<pre><font color="#008000">    CMyClass(Type arg) { data = arg; }</font></pre>

<pre><font color="#008000">    ~CMyClass() {};</font></pre>

<pre><font color="#008000">};</font></pre>

<P>When you're ready to instantiate objects from the template class, you must supply the data type that will replace the template parameters. For example, to create an object of the <font color="#008000">CMyClass</font> class, you might use a line like 
this:</P>

<pre><font color="#008000">CMyClass&lt;int&gt; myClass(15);</font></pre>

<P>The previous line creates a <font color="#008000">CMyClass</font> object that uses integers in place of the abstract data type. If you wanted the class to deal with floating-point values, you'd create an object of the class something like this:</P>

<pre><font color="#008000">CMyClass&lt;float&gt; myClass(15.75);</font></pre>

<P>For a more complete example, suppose you want to create a class that stores two values and has member functions that compare those values. Listing 26.11 is a program that does just that. First, the listing defines a class template called <font 
color="#008000">CCompare</font>. This class stores two values that are supplied to the constructor. The class also includes the usual constructor and destructor, as well as member functions for determining the larger or smaller of the values, or to 
determine whether the values are equal.</P>

<P><I>Listing 26.11&#151;TEMPLATE3.CPP&#151;Using a Class Template</I></P>

<pre><font color="#008000">#include &lt;iostream.h&gt;</font></pre>

<pre><font color="#008000">template&lt;class Type&gt;</font></pre>

<pre><font color="#008000">class CCompare</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">protected:</font></pre>

<pre><font color="#008000">    Type arg1;</font></pre>

<pre><font color="#008000">    Type arg2;</font></pre>

<pre><font color="#008000">public:</font></pre>

<pre><font color="#008000">    CCompare(Type arg1, Type arg2)</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">        CCompare::arg1 = arg1;</font></pre>

<pre><font color="#008000">        CCompare::arg2 = arg2;</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">    ~CCompare() {}</font></pre>

<pre><font color="#008000">    Type GetMin()</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">        Type min;</font></pre>

<pre><font color="#008000">        if (arg1 &lt; arg2)</font></pre>

<pre><font color="#008000">            min = arg1;</font></pre>

<pre><font color="#008000">        else</font></pre>

<pre><font color="#008000">            min = arg2;</font></pre>

<pre><font color="#008000">        return min;</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">    Type GetMax()</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">        Type max;</font></pre>

<pre><font color="#008000">        if (arg1 &gt; arg2)</font></pre>

<pre><font color="#008000">            max = arg1;</font></pre>

<pre><font color="#008000">        else</font></pre>

<pre><font color="#008000">            max = arg2;</font></pre>

<pre><font color="#008000">        return max;</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">    int Equal()</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">        int equal;</font></pre>

<pre><font color="#008000">        if (arg1 == arg2)</font></pre>

<pre><font color="#008000">            equal = 1;</font></pre>

<pre><font color="#008000">        else</font></pre>

<pre><font color="#008000">            equal = 0;</font></pre>

<pre><font color="#008000">        return equal;</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">};</font></pre>

<pre><font color="#008000">int main()</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    CCompare&lt;int&gt; compare1(15, 25);</font></pre>

<pre><font color="#008000">    CCompare&lt;double&gt; compare2(254.78, 12.983);</font></pre>

<pre><font color="#008000">    CCompare&lt;char&gt; compare3('A', 'Z');</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;THE COMPARE1 OBJECT&quot; &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Lowest: &quot; &lt;&lt; compare1.GetMin() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Highest: &quot; &lt;&lt; compare1.GetMax() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Equal: &quot; &lt;&lt; compare1.Equal() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;THE COMPARE2 OBJECT&quot; &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Lowest: &quot; &lt;&lt; compare2.GetMin() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Highest: &quot; &lt;&lt; compare2.GetMax() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Equal: &quot; &lt;&lt; compare2.Equal() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;THE COMPARE2 OBJECT&quot; &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Lowest: &quot; &lt;&lt; compare3.GetMin() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Highest: &quot; &lt;&lt; compare3.GetMax() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; &quot;Equal: &quot; &lt;&lt; compare3.Equal() &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    cout &lt;&lt; endl;</font></pre>

<pre><font color="#008000">    return 0;</font></pre>

<pre><font color="#008000">}</font></pre>

<P>The main program instantiates three objects from the class template, one that deals with integers, one that uses floating-point values, and one that stores and compares character values. After creating the three <font color="#008000">CCompare</font> 
objects, <font color="#008000">main()</font> calls the objects' member functions in order to display information about the data stored in each object. Figure 26.1 shows the program's output.</P>

<A HREF="AAfig01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch26/AAfig01.gif"><b>Fig. 26.1</b></A>

<P><I>The Template3 program creates three different objects from a class </I><I>template.</I></P>

⌨️ 快捷键说明

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