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

📄 ch14.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 4 页
字号:
31:                Square(valOne,valTwo);32:                PrintVals(valOne, valTwo);33:                break;34:35:             case 3:36:                PrintVals(valOne, valTwo);37:                Cube(valOne, valTwo);38:                PrintVals(valOne, valTwo);39:                break;40:41:             case 4:42:                PrintVals(valOne, valTwo);43:                Swap(valOne, valTwo);44:                PrintVals(valOne, valTwo);45:                break;46:47:                default :48:                fQuit = TRUE;49:                break;50:          }51: 52:          if (fQuit)53:             break;54:       }55:     return 0;<TT>56: }</TT></FONT><FONT COLOR="#0066FF">Output: (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 1x: 1 y: 2New value for ValOne: 2New value for ValTwo: 3(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3x: 2 y: 3x: 8 y: 27(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2x: 8 y: 27x: 64 y: 729(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4x: 64 y: 729x: 729 y: 64(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 0</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>The implementation of thefunctions has been left out, because it is identical to that provided in Listing14.5. As you can see, the output is unchanged, but the body of the program has expandedfrom 27 lines to 38. The calls to <TT>PrintVals()</TT> must be repeated for eachcase.</P><P>It was tempting to put <TT>PrintVals()</TT> at the top of the <TT>while</TT> loopand again at the bottom, rather than in each case statement. This would have called<TT>PrintVals()</TT> even for the exit case, however, and that was not part of thespecification.</P><P>Setting aside the increased size of the code and the repeated calls to do thesame thing, the overall clarity is somewhat diminished. This is an artificial case,however, created to show how pointers to functions work. In real-world conditionsthe advantages are even clearer: pointers to functions can eliminate duplicate code,clarify your program, and allow you to make tables of functions to call based onruntime conditions.<H3 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">Shorthand Invocation</FONT></H3><P>The pointer to function does not need to be dereferenced, though you are freeto do so. Therefore, if <TT>pFunc</TT> is a pointer to a function taking an integerand returning a variable of type <TT>long</TT>, and you assign <TT>pFunc</TT> toa matching function, you can invoke that function with either</P><PRE><FONT COLOR="#0066FF">pFunc(x);</FONT></PRE><P>or</P><PRE><FONT COLOR="#0066FF">(*pFunc)(x);</FONT></PRE><P>The two forms are identical. The former is just a shorthand version of the latter.<H4 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Arrays of Pointersto Functions</FONT></H4><P>Just as you can declare an array of pointers to integers, you can declare an arrayof pointers to functions returning a specific value type and with a specific signature.Listing 14.7 again rewrites Listing 14.5, this time using an array to invoke allthe choices at once.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>To compile this program, place lines	41-80 of Listing 14.5 immediately after line 39. <HR></BLOCKQUOTE><P><A NAME="Heading22"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.7. Demonstratesuse of an array of pointers to functions.</B></FONT><PRE><FONT COLOR="#0066FF">1:     // Listing 14.7 demonstrates use of an array of pointers to functions2:3:     #include &lt;iostream.h&gt;4:5:     void Square (int&amp;,int&amp;);6:     void Cube (int&amp;, int&amp;);7:     void Swap (int&amp;, int &amp;);8:     void GetVals(int&amp;, int&amp;);9:     void PrintVals(int, int);10:    enum BOOL { FALSE, TRUE };11:12:    int main()13:    {14:       int valOne=1, valTwo=2;15:       int choice, i;16:       const MaxArray = 5;17:       void (*pFuncArray[MaxArray])(int&amp;, int&amp;);18:19:       for (i=0;i&lt;MaxArray;i++)20:       {21:          cout &lt;&lt; &quot;(1)Change Values (2)Square (3)Cube (4)Swap: &quot;;22:          cin &gt;&gt; choice;23:          switch (choice)24:          {25:             case 1:pFuncArray[i] = GetVals; break;26:             case 2:pFuncArray[i] = Square; break;27:             case 3:pFuncArray[i] = Cube; break;28:             case 4:pFuncArray[i] = Swap; break;29:             default:pFuncArray[i] = 0;30:          }31:       }32:33:       for (i=0;i&lt;MaxArray; i++)34:       {35:          pFuncArray[i](valOne,valTwo);36:          PrintVals(valOne,valTwo);37:       }38:     return 0;<TT>39: }</TT></FONT><FONT COLOR="#0066FF">Output: (1)Change Values (2)Square (3)Cube (4)Swap: 1(1)Change Values (2)Square (3)Cube (4)Swap: 2(1)Change Values (2)Square (3)Cube (4)Swap: 3(1)Change Values (2)Square (3)Cube (4)Swap: 4(1)Change Values (2)Square (3)Cube (4)Swap: 2New Value for ValOne: 2New Value for ValTwo: 3x: 2 y: 3x: 4 y: 9x: 64 y: 729x: 729 y: 64x: 7153 y:4096</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Once again the implementationof the functions has been left out to save space, but it is the same as in Listing14.5. On line 17, the array <TT>pFuncArray</TT> is de- clared to be an array of 5pointers to functions that return <TT>void</TT> and that take two integer references.</P><P>On lines 19-31, the user is asked to pick the functions to invoke, and each memberof the array is assigned the address of the appropriate function. On lines 33-37,each function is invoked in turn. The result is printed after each invocation.<H4 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Passing Pointersto Functions to Other Functions</FONT></H4><P>The pointers to functions (and arrays of pointers to functions, for that matter)can be passed to other functions, which may take action and then call the right functionusing the pointer.</P><P>For example, you might improve Listing 14.5 by passing the chosen function pointerto another function (outside of <TT>main()</TT>), which prints the values, invokesthe function, and then prints the values again. Listing 14.8 illustrates this variation.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>To compile this program, place	lines 46-80 of Listing 14.5 immediately after line 45. <HR></BLOCKQUOTE><P><A NAME="Heading25"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.8. Passingpointers to functions as function arguments.</B></FONT><PRE><FONT COLOR="#0066FF">1:     // Listing 14.8 Without function pointers2:3:     #include &lt;iostream.h&gt;4:5:     void Square (int&amp;,int&amp;);6:     void Cube (int&amp;, int&amp;);7:     void Swap (int&amp;, int &amp;);8:     void GetVals(int&amp;, int&amp;);9:     void PrintVals(void (*)(int&amp;, int&amp;),int&amp;, int&amp;);10:    enum BOOL { FALSE, TRUE };11:12:    int main()13:    {14:       int valOne=1, valTwo=2;15:       int choice;16:       BOOL fQuit = FALSE;17:18:       void (*pFunc)(int&amp;, int&amp;);19:20:       while (fQuit == FALSE)21:       {22:          cout &lt;&lt; &quot;(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: &quot;;23:          cin &gt;&gt; choice;24:          switch (choice)25:          {26:             case 1:pFunc = GetVals; break;27:             case 2:pFunc = Square; break;28:             case 3:pFunc = Cube; break;29:             case 4:pFunc = Swap; break;30:             default:fQuit = TRUE; break;31:          }32:          if (fQuit == TRUE)33:             break;34:          PrintVals ( pFunc, valOne, valTwo);35:       }36:37:     return 0;38:    }39:40:    void PrintVals( void (*pFunc)(int&amp;, int&amp;),int&amp; x, int&amp; y)41:    {42:       cout &lt;&lt; &quot;x: &quot; &lt;&lt; x &lt;&lt; &quot; y: &quot; &lt;&lt; y &lt;&lt; endl;43:       pFunc(x,y);44:       cout &lt;&lt; &quot;x: &quot; &lt;&lt; x &lt;&lt; &quot; y: &quot; &lt;&lt; y &lt;&lt; endl;<TT>45: }</TT></FONT><FONT COLOR="#0066FF">Output: (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 1x: 1 y: 2New value for ValOne: 2New value for ValTwo: 3x: 2 y: 3(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3x: 2 y: 3x: 8 y: 27(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2x: 8 y: 27x: 64 y: 729(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4x: 64 y: 729x: 729 y:64(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 0</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>On line 18, <TT>pFunc</TT>is declared to be a pointer to a function returning <TT>void</TT> and taking twoparameters, both integer references. On line 9, <TT>PrintVals</TT> is declared tobe a function taking three parameters. The first is a pointer to a function thatreturns <TT>void</TT> but takes two integer reference parameters, and the secondand third arguments to <TT>PrintVals</TT> are integer references. The user is againprompted for which functions to call, and then on line 34 <TT>PrintVals</TT> is called.</P><P>Go find a C++ programmer and ask him what this declaration means:</P><PRE><FONT COLOR="#0066FF">void PrintVals(void (*)(int&amp;, int&amp;),int&amp;, int&amp;);</FONT></PRE><P>This is the kind of declaration that you use infrequently and probably look upin the book each time you need it, but it will save your program on those rare occasionswhen it is exactly the required construct.<H4 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">Using typedef withPointers to Functions</FONT></H4><P>The construct <TT>void (*)(int&amp;, int&amp;)</TT> is cumbersome, at best. Youcan use <TT>typedef</TT> to simplify this, by declaring a type <TT>VPF</TT> as apointer to a function returning void and taking two integer references. Listing 14.9rewrites Listing 14.8 using this <TT>typedef</TT> statement.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE: </B></FONT>To compile this program, place lines 46-80	of Listing 14.5 immediately after line 45. <HR></BLOCKQUOTE><P><A NAME="Heading28"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.9. Usingtypedef to make pointers to functions more readable.</B></FONT><PRE><FONT COLOR="#0066FF">1:   // Listing 14.9. Using typedef to make pointers to functions more _readable2:3:   #include &lt;iostream.h&gt;4:5:   void Square (int&amp;,int&amp;);6:   void Cube (int&amp;, int&amp;);7:   void Swap (int&amp;, int &amp;);8:   void GetVals(int&amp;, int&amp;);9:   typedef  void (*VPF) (int&amp;, int&amp;) ;10:   void PrintVals(VPF,int&amp;, int&amp;);11:   enum BOOL { FALSE, TRUE };12:13:   int main()14:   {15:   int valOne=1, valTwo=2;16:   int choice;17:   BOOL fQuit = FALSE;18:19:   VPF pFunc;20:21:   while (fQuit == FALSE)22:   {23:   cout &lt;&lt; &quot;(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: &quot;;24:   cin &gt;&gt; choice;25:   switch (choice)26:   {27:   case 1:pFunc = GetVals; break;28:   case 2:pFunc = Square; break;29:   case 3:pFunc = Cube; break;30:   case 4:pFunc = Swap; break;31:   default:fQuit = TRUE; break;32:   }33:   if (fQuit == TRUE)34:   break;35:   PrintVals ( pFunc, valOne, valTwo);36:   }37:   return 0;38:   }39:40:   void PrintVals( VPF pFunc,int&amp; x, int&amp; y)41:   {42:   cout &lt;&lt; &quot;x: &quot; &lt;&lt; x &lt;&lt; &quot; y: &quot; &lt;&lt; y &lt;&lt; endl;43:   pFunc(x,y);44:   cout &lt;&lt; &quot;x: &quot; &lt;&lt; x &lt;&lt; &quot; y: &quot; &lt;&lt; y &lt;&lt; endl;<TT>45: }</TT></FONT><FONT COLOR="#0066FF">Output: (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 1x: 1 y: 2New value for ValOne: 2New value for ValTwo: 3x: 2 y: 3(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3x: 2 y: 3x: 8 y: 27(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2x: 8 y: 27x: 64 y: 729(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4x: 64 y: 729x: 729 y: 64(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 0</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>On line 9, <TT>typedef</TT>is used to declare <TT>VPF</TT> to be of the type &quot;function that returns <TT>void</TT>and takes two parameters, both integer references.&quot;</P><P>On line 10, the function <TT>PrintVals()</TT> is declared to take three parameters:a <TT>VPF</TT> and two integer references. On line 19, <TT>pFunc</TT> is now declared

⌨️ 快捷键说明

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