举例说明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持久

2010-06-22 13:08:42

Linux At命令

2018-11-28 08:20:15

Linuxalias命令

2010-03-03 10:55:39

2010-03-04 13:21:32

linux压缩命令

2010-01-27 11:00:17

C++操作符

2010-06-18 10:24:51

Linux acces

2009-12-07 17:28:55

WCF数据

2010-03-04 13:45:37

Linux压缩命令

2009-09-11 09:13:34

2010-01-06 16:54:07

.Net Framew

2010-04-09 16:52:36

Unix操作系统

2010-01-06 10:35:02

Json_Decode

2010-09-24 17:39:28

SQL中EXISTS

2010-01-20 10:19:16

VB.NET动态接口

2010-01-19 17:54:47

C++程序
点赞
收藏

51CTO技术栈公众号