举例说明C 编写程序
创始人
2024-06-21 06:00:27
0

进行C++编写程序时,你经常需要在一个函数中调用其他函数,此时就会考虑到使用函数指针,一个函数可以调用其他函数。在设计良好的程序中,每个函数都有特定的目的,普通函数指针的使用。

首先让我们来看下面的一个例子:

  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. // 使用函数指针作为参数,默认参数为&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作为函数参数的代码。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...