Example: C ++ Programming

Source: Internet
Author: User

When writing a program in C ++, you often need to call other functions in a function. In this case, you will consider using a function pointer. A function can call other functions. In a well-designed program, each function has a specific purpose, the use of common function pointers.

First, let's look at the following example:

 
 
  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 (intI=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 (intI=0; I< 5; ++ I ){
  88.  
  89. DoSomethingAboutRed ();
  90.  
  91. }
  92.  
  93. }
  94.  
  95. Else if (IsGreen (color )){
  96.  
  97. For (intI=0; I< 5; ++ I ){
  98.  
  99. DoSomethingAboutGreen ();
  100.  
  101. }
  102.  
  103. }
  104.  
  105. Else if (IsBlue (color )){
  106.  
  107. For (intI=0; I< 5; ++ I ){
  108.  
  109. DoSomethingAboutBlue ();
  110.  
  111. }
  112.  
  113. }
  114.  
  115. Else return;
  116.  
  117. }
  118.  
  119.  
  120. // Use the function pointer as a parameter. The default parameter is & IsBlue
  121.  
  122. Void DoSomethingC (void (* DoSomethingAboutColor) () = & DoSomethingAboutBlue ){
  123.  
  124. For (intI=0; I< 5; ++ I)
  125.  
  126. {
  127.  
  128. DoSomethingAboutColor ();
  129.  
  130. }
  131.  
  132. }

We can see that in the DoSomethingA function, the color value needs to be determined once for each loop. These are repeated judgments. In C ++ programming, the for loop is repeatedly written three times, the code is not refined. If we use the function pointer here, we can judge the color value only once and write the for loop only once. DoSomethingC provides the code that uses the function pointer as the function parameter, doSomethingD provides the code that uses string as the function parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.