日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
舉例說明C++編寫程序

進(jìn)行C++編寫程序時,你經(jīng)常需要在一個函數(shù)中調(diào)用其他函數(shù),此時就會考慮到使用函數(shù)指針,一個函數(shù)可以調(diào)用其他函數(shù)。在設(shè)計良好的程序中,每個函數(shù)都有特定的目的,普通函數(shù)指針的使用。

創(chuàng)新互聯(lián)主營樂平網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),樂平h5微信小程序搭建,樂平網(wǎng)站營銷推廣歡迎樂平等地區(qū)企業(yè)咨詢

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

 
 
 
  1. #include  
  2.  
  3. #include  
  4.  
  5. #include  
  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. // 使用函數(shù)指針作為參數(shù),默認(rèn)參數(shù)為&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函數(shù)中,每次循環(huán)都需要判斷一次color的值,這些屬于重復(fù)判斷;在C++編寫程序中,for 循環(huán)重復(fù)寫了三次,代碼不夠精練。如果我們在這里使用函數(shù)指針,就可以只判斷一次color的值,并且for 循環(huán)也只寫一次,DoSomethingC給出了使用函數(shù)指針作為函數(shù)參數(shù)的代碼,而DoSomethingD給出了使用string作為函數(shù)參數(shù)的代碼。


文章名稱:舉例說明C++編寫程序
URL鏈接:http://www.dlmjj.cn/article/djdeeee.html