成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

舉例說明C++編寫程序

開發 后端
在進行C++編寫程序設計時使用函數指針可以使得您的代碼更加精練,下面就用充分的案例來講述C++編寫程序的問題。。

進行C++編寫程序時,你經常需要在一個函數中調用其他函數,此時就會考慮到使用函數指針,一個函數可以調用其他函數。在設計良好的程序中,每個函數都有特定的目的,普通函數指針的使用。

首先讓我們來看下面的一個例子:

  1. #include <string> 
  2.  
  3. #include <vector> 
  4.  
  5. #include <iostream> 
  6.  
  7. using namespace std;  
  8.  
  9.  
  10. bool IsRed( string color ) {  
  11.  
  12. return ( color == "red" );  
  13.  
  14. }  
  15.  
  16.  
  17. bool IsGreen( string color ) {  
  18.  
  19. return ( color == "green" );  
  20.  
  21. }  
  22.  
  23.  
  24. bool IsBlue( string color ) {  
  25.  
  26. return ( color == "blue" );  
  27.  
  28. }  
  29.  
  30.  
  31. void DoSomethingAboutRed() {  
  32.  
  33. cout << "The complementary color of red is cyan!\n";  
  34.  
  35. }  
  36.  
  37.  
  38. void DoSomethingAboutGreen() {  
  39.  
  40. cout << "The complementary color of green is magenta!\n";  
  41.  
  42. }  
  43.  
  44.  
  45. void DoSomethingAboutBlue() {  
  46.  
  47. cout << "The complementary color of blue is yellow!\n";  
  48.  
  49. }  
  50.  
  51.  
  52. void DoSomethingA( string color ) {  
  53.  
  54. for ( int i = 0; i < 5; ++i )  
  55.  
  56. {  
  57.  
  58. if ( IsRed( color ) ) {  
  59.  
  60. DoSomethingAboutRed();  
  61.  
  62. }  
  63.  
  64. else if ( IsGreen( color ) ) {  
  65.  
  66. DoSomethingAboutGreen();  
  67.  
  68. }  
  69.  
  70. else if ( IsBlue( color) ) {  
  71.  
  72. DoSomethingAboutBlue();  
  73.  
  74. }  
  75.  
  76. else return;          
  77.  
  78. }  
  79.  
  80. }  
  81.  
  82.  
  83. void DoSomethingB( string color ) {  
  84.  
  85. if ( IsRed( color ) ) {  
  86.  
  87. for ( int i = 0; i < 5; ++i ) {  
  88.  
  89. DoSomethingAboutRed();  
  90.  
  91. }  
  92.  
  93. }  
  94.  
  95. else if ( IsGreen( color ) ) {  
  96.  
  97. for ( int i = 0; i < 5; ++i ) {  
  98.  
  99. DoSomethingAboutGreen();  
  100.  
  101. }  
  102.  
  103. }  
  104.  
  105. else if ( IsBlue( color) ) {  
  106.  
  107. for ( int i = 0; i < 5; ++i ) {  
  108.  
  109. DoSomethingAboutBlue();  
  110.  
  111. }  
  112.  
  113. }  
  114.  
  115. else return;  
  116.  
  117. }  
  118.  
  119.  
  120. // 使用函數指針作為參數,默認參數為&IsBlue  
  121.  
  122. void DoSomethingC( void (*DoSomethingAboutColor)() = &DoSomethingAboutBlue ) {  
  123.  
  124. for ( int i = 0; i < 5; ++i )  
  125.  
  126. {  
  127.  
  128. DoSomethingAboutColor();  
  129.  
  130. }  
  131.  

可以看到在DoSomethingA函數中,每次循環都需要判斷一次color的值,這些屬于重復判斷;在C++編寫程序中,for 循環重復寫了三次,代碼不夠精練。如果我們在這里使用函數指針,就可以只判斷一次color的值,并且for 循環也只寫一次,DoSomethingC給出了使用函數指針作為函數參數的代碼,而DoSomethingD給出了使用string作為函數參數的代碼。

責任編輯:chenqingxiang 來源: pcppc.cn
相關推薦

2010-01-08 17:06:52

C++代碼

2010-01-21 09:53:23

C++操作符

2010-01-12 15:56:25

C++軟件

2013-11-26 11:08:23

Linux命令diff

2009-09-25 09:30:33

Hibernate持久

2018-11-28 08:20:15

Linuxalias命令

2010-06-22 13:08:42

Linux At命令

2010-01-27 11:00:17

C++操作符

2010-03-03 10:55:39

2010-03-04 13:21:32

linux壓縮命令

2010-06-18 10:24:51

Linux acces

2009-09-11 09:13:34

2009-12-07 17:28:55

WCF數據

2010-03-04 13:45:37

Linux壓縮命令

2010-01-06 16:54:07

.Net Framew

2010-04-09 16:52:36

Unix操作系統

2010-01-19 17:54:47

C++程序

2010-01-06 10:35:02

Json_Decode

2010-01-20 10:19:16

VB.NET動態接口

2010-09-24 17:39:28

SQL中EXISTS
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 涩爱av一区二区三区 | 亚洲福利精品 | 日一区二区 | 欧美理论片在线 | 午夜免费电影 | 一区二区免费在线视频 | 精品99久久久久久 | 免费精品视频一区 | 色狠狠一区 | 日韩激情视频一区 | 亚洲第一视频 | 91在线色视频 | 国内精品视频在线 | 亚洲激情在线观看 | 久久精品免费观看 | 在线不卡视频 | 艹逼网| 久久精品视频免费观看 | 日韩中文字幕一区二区 | 亚洲人成网站777色婷婷 | 中文字幕 亚洲一区 | 正在播放一区二区 | 国产精品久久久久久久久久妇女 | 五月天婷婷丁香 | 亚洲欧美一区二区三区视频 | 精品一区久久 | 成人精品 | 一区二区在线 | 国产欧美日韩综合精品一 | 国产精品久久久久久久久动漫 | 伊人色综合久久天天五月婷 | 男人天堂免费在线 | 欧美日韩国产一区二区三区 | 亚洲精品一区国语对白 | 亚洲欧美成人在线 | 国产视频第一页 | 欧美一区二区三区的 | 在线不卡一区 | 欧美一区二区在线 | 久久综合影院 | 免费观看羞羞视频网站 |