亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? 014.htm

?? 21天內快速掌握c++語言
?? HTM
?? 第 1 頁 / 共 4 頁
字號:
29:             case 2:
30:                PrintVals(valOne, valTwo);
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: 1
x: 1 y: 2
New value for ValOne: 2
New value for ValTwo: 3
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3
x: 2 y: 3
x: 8 y: 27
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2
x: 8 y: 27
x: 64 y: 729
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4
x: 64 y: 729
x: 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 the
functions has been left out, because it is identical to that provided in Listing
14.5. As you can see, the output is unchanged, but the body of the program has expanded
from 27 lines to 38. The calls to <TT>PrintVals()</TT> must be repeated for each
case.</P>
<P>It was tempting to put <TT>PrintVals()</TT> at the top of the <TT>while</TT> loop
and 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 the
specification.</P>
<P>Setting aside the increased size of the code and the repeated calls to do the
same thing, the overall clarity is somewhat diminished. This is an artificial case,
however, created to show how pointers to functions work. In real-world conditions
the 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 on
runtime 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 free
to do so. Therefore, if <TT>pFunc</TT> is a pointer to a function taking an integer
and returning a variable of type <TT>long</TT>, and you assign <TT>pFunc</TT> to
a matching function, you can invoke that function with either</P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">pFunc(x);
</FONT></PRE>
<P>or</P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">(*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 Pointers
to Functions</FONT></H4>
<P>Just as you can declare an array of pointers to integers, you can declare an array
of 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 all
the choices at once.
<BR>
<BR>
<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>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<P><A NAME="Heading22"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.7. Demonstrates
use of an array of pointers to functions.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1:     // Listing 14.7 demonstrates use of an array of pointers to functions
2:
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: 2
New Value for ValOne: 2
New Value for ValTwo: 3
x: 2 y: 3
x: 4 y: 9
x: 64 y: 729
x: 729 y: 64
x: 7153 y:4096
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Once again the implementation
of the functions has been left out to save space, but it is the same as in Listing
14.5. On line 17, the array <TT>pFuncArray</TT> is de- clared to be an array of 5
pointers 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 member
of 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 Pointers
to 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 function
using the pointer.</P>
<P>For example, you might improve Listing 14.5 by passing the chosen function pointer
to another function (outside of <TT>main()</TT>), which prints the values, invokes
the function, and then prints the values again. Listing 14.8 illustrates this variation.
<BR>
<BR>
<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>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<P><A NAME="Heading25"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.8. Passing
pointers to functions as function arguments.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1:     // Listing 14.8 Without function pointers
2:
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: 1
x: 1 y: 2
New value for ValOne: 2
New value for ValTwo: 3
x: 2 y: 3
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3
x: 2 y: 3
x: 8 y: 27
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2
x: 8 y: 27
x: 64 y: 729
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4
x: 64 y: 729
x: 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 two
parameters, both integer references. On line 9, <TT>PrintVals</TT> is declared to
be a function taking three parameters. The first is a pointer to a function that
returns <TT>void</TT> but takes two integer reference parameters, and the second
and third arguments to <TT>PrintVals</TT> are integer references. The user is again
prompted 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" font style="font-size:10pt">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 up
in the book each time you need it, but it will save your program on those rare occasions
when it is exactly the required construct.
<H4 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">Using typedef with
Pointers to Functions</FONT></H4>
<P>The construct <TT>void (*)(int&amp;, int&amp;)</TT> is cumbersome, at best. You
can use <TT>typedef</TT> to simplify this, by declaring a type <TT>VPF</TT> as a
pointer to a function returning void and taking two integer references. Listing 14.9
rewrites Listing 14.8 using this <TT>typedef</TT> statement.
<BR>
<BR>
<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>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<P><A NAME="Heading28"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.9. Using
typedef to make pointers to functions more readable.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1:   // Listing 14.9. Using typedef to make pointers to functions more _readable
2:
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: 1
x: 1 y: 2
New value for ValOne: 2
New value for ValTwo: 3
x: 2 y: 3
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3
x: 2 y: 3
x: 8 y: 27
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2
x: 8 y: 27
x: 64 y: 729
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4
x: 64 y: 729
x: 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>

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久蜜桃av一区精品变态类天堂| 国产精品免费久久| 亚洲国产高清在线| 日韩电影在线一区二区三区| 国产成人在线色| 日韩欧美一区在线观看| 国产精品久久久久久久久搜平片| 青青草成人在线观看| 91色|porny| 欧美国产激情一区二区三区蜜月| 日本不卡在线视频| 在线视频你懂得一区二区三区| 国产亚洲一区二区在线观看| 久久av中文字幕片| 777午夜精品免费视频| 一区二区三区影院| 粉嫩av一区二区三区粉嫩| 日韩欧美一二区| 日日骚欧美日韩| 欧美军同video69gay| 亚洲精品国产第一综合99久久| 黄色资源网久久资源365| 欧美一二区视频| 日韩成人一级片| 欧美日韩国产综合一区二区 | 色综合久久久久| 国产精品剧情在线亚洲| 国产成人精品三级| 国产女同性恋一区二区| 福利一区福利二区| 国产精品久久久久久久久快鸭| 国产精一区二区三区| 精品99999| 国产精品中文欧美| 中文字幕国产一区二区| 不卡av免费在线观看| 成人欧美一区二区三区在线播放| 91丨九色丨国产丨porny| 亚洲图片你懂的| 欧美性色aⅴ视频一区日韩精品| 亚洲综合自拍偷拍| 欧美视频一区二区三区四区| 亚洲chinese男男1069| 日韩欧美一级在线播放| 国产一区二区中文字幕| 国产精品久久网站| 色先锋久久av资源部| 亚洲福中文字幕伊人影院| 欧美日韩电影在线播放| 欧美aaaaaa午夜精品| 精品国产免费视频| gogogo免费视频观看亚洲一| 一级特黄大欧美久久久| 7777精品伊人久久久大香线蕉经典版下载 | 奇米精品一区二区三区在线观看一| 欧美电影一区二区| 精品一区二区在线视频| 中文字幕欧美日韩一区| 欧美手机在线视频| 久久99国产精品免费网站| 中文字幕精品三区| 欧美伊人久久大香线蕉综合69| 日本va欧美va瓶| 欧美极品另类videosde| 欧美视频一区二区三区四区 | 久久免费的精品国产v∧| 色综合中文字幕| 免费成人小视频| 中文字幕制服丝袜成人av| 欧美日韩成人在线| 成人蜜臀av电影| 日韩va欧美va亚洲va久久| 中文字幕在线不卡视频| 国产精品视频一二三区| 欧美美女一区二区| 成人性生交大片免费看中文| 日韩中文字幕1| 国产精品久久久久久久蜜臀 | 欧美三片在线视频观看| 国产高清在线精品| 日韩电影一区二区三区| 亚洲免费观看视频| 国产日韩欧美麻豆| 日韩一级大片在线观看| 91网站最新地址| 韩国在线一区二区| 日韩av中文字幕一区二区| 亚洲精选视频在线| 亚洲国产精品av| 久久先锋资源网| 日韩免费在线观看| 91精品欧美综合在线观看最新| 色欧美乱欧美15图片| 成人免费视频caoporn| 狠狠狠色丁香婷婷综合激情| 免费的成人av| 日日夜夜免费精品| 亚洲国产精品久久久久婷婷884| 国产精品日韩精品欧美在线| 精品国产一区二区精华| 91精品国产色综合久久不卡蜜臀| 日本福利一区二区| 99国产精品一区| 99热精品国产| 91在线你懂得| 91在线国产福利| 91丝袜美女网| 色婷婷一区二区三区四区| 99re热这里只有精品免费视频| 福利电影一区二区| 床上的激情91.| 本田岬高潮一区二区三区| 成人性视频网站| 在线看日本不卡| 色综合久久综合网97色综合| av高清久久久| 色播五月激情综合网| 欧美在线啊v一区| 欧美福利视频导航| 欧美日韩亚洲另类| 欧美精品三级在线观看| 在线不卡欧美精品一区二区三区| 欧美日韩电影一区| 91精品国产一区二区三区香蕉| 欧美一区二区视频观看视频| 精品成人a区在线观看| 精品国产网站在线观看| 国产午夜三级一区二区三| 国产精品美女一区二区在线观看| 亚洲视频在线观看一区| 亚洲嫩草精品久久| 婷婷综合五月天| 另类小说一区二区三区| 国产盗摄一区二区| 91网站最新地址| 3atv在线一区二区三区| 久久久久国产精品麻豆| 亚洲人快播电影网| 日韩和欧美一区二区| 经典三级一区二区| 丰满少妇久久久久久久| 日本福利一区二区| 日韩精品一区二区在线| 国产日产欧美一区二区视频| 一区二区三区中文免费| 日韩经典一区二区| 国产成人在线免费| 欧美日韩一区不卡| 久久亚洲私人国产精品va媚药| 亚洲人成精品久久久久| 另类的小说在线视频另类成人小视频在线 | 久久久99久久| 一区二区三区四区在线播放| 美女在线一区二区| 91视视频在线观看入口直接观看www | wwwwxxxxx欧美| 亚洲激情第一区| 国产综合成人久久大片91| 94-欧美-setu| 精品国产免费一区二区三区四区| 一区二区三区中文字幕精品精品| 视频精品一区二区| 91麻豆福利精品推荐| 日韩一级片在线播放| √…a在线天堂一区| 国内精品伊人久久久久av一坑| 色域天天综合网| 国产婷婷色一区二区三区在线| 性做久久久久久久免费看| 成人午夜av电影| 欧美精品一区二区三区四区| 亚洲综合区在线| www.色精品| 久久网站热最新地址| 午夜国产精品影院在线观看| 99精品视频一区二区三区| 久久久久久久久久久电影| 蜜臀精品久久久久久蜜臀 | 成人av综合一区| 26uuu久久天堂性欧美| 日本不卡一二三| 欧美乱妇23p| 亚洲成人黄色小说| 在线亚洲一区观看| 亚洲欧美视频一区| 91丝袜美女网| 日韩码欧中文字| 99久久久久免费精品国产 | 亚洲成a天堂v人片| 一本色道久久综合亚洲精品按摩| 国产精品乱码人人做人人爱| 国产精品自拍在线| 2023国产一二三区日本精品2022| 免费看欧美美女黄的网站| 6080亚洲精品一区二区| 日韩成人av影视| 在线综合视频播放| 免费成人在线观看| 欧美精品一区二区三区一线天视频| 久久精品二区亚洲w码|