自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

舉例說(shuō)明C++編寫(xiě)程序

開(kāi)發(fā) 后端
在進(jìn)行C++編寫(xiě)程序設(shè)計(jì)時(shí)使用函數(shù)指針可以使得您的代碼更加精練,下面就用充分的案例來(lái)講述C++編寫(xiě)程序的問(wèn)題。。

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

首先讓我們來(lái)看下面的一個(gè)例子:

  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. // 使用函數(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++編寫(xiě)程序中,for 循環(huán)重復(fù)寫(xiě)了三次,代碼不夠精練。如果我們?cè)谶@里使用函數(shù)指針,就可以只判斷一次color的值,并且for 循環(huán)也只寫(xiě)一次,DoSomethingC給出了使用函數(shù)指針作為函數(shù)參數(shù)的代碼,而DoSomethingD給出了使用string作為函數(shù)參數(shù)的代碼。

責(zé)任編輯:chenqingxiang 來(lái)源: pcppc.cn
相關(guān)推薦

2010-01-08 17:06:52

C++代碼

2010-01-12 15:56:25

C++軟件

2010-01-21 09:53:23

C++操作符

2013-11-26 11:08:23

Linux命令diff

2018-11-28 08:20:15

Linuxalias命令

2010-06-22 13:08:42

Linux At命令

2009-09-25 09:30:33

Hibernate持久

2010-01-27 11:00:17

C++操作符

2010-06-18 10:24:51

Linux acces

2010-03-03 10:55:39

2010-03-04 13:21:32

linux壓縮命令

2009-09-11 09:13:34

2010-01-06 16:54:07

.Net Framew

2009-12-07 17:28:55

WCF數(shù)據(jù)

2010-03-04 13:45:37

Linux壓縮命令

2010-04-09 16:52:36

Unix操作系統(tǒng)

2010-01-20 10:19:16

VB.NET動(dòng)態(tài)接口

2010-09-24 17:39:28

SQL中EXISTS

2010-01-19 17:54:47

C++程序

2010-01-06 10:35:02

Json_Decode
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)