Software Best Practices

Voices on Software Development Best Practices
Welcome to Software Best Practices Sign in | Join | Help
in Search

Use of Function pointers

Last post 05-23-2008 2:14 PM by Ralph Trickey. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 05-09-2008 5:10 AM

    Use of Function pointers

    Hi all,

     I found in a tutorial on function pointers that apart from being used for Callback functions, the function pointers can also be used to replace switch statments. Please find below the example code for the same :

    //------------------------------------------------------------------------------------
    // 1.2 Introductory Example or How to Replace a Switch-Statement
    // Task: Perform one of the four basic arithmetic operations specified by the
    //       characters '+', '-', '*' or '/'.
    
    
    // The four arithmetic operations ... one of these functions is selected
    // at runtime with a swicth or a function pointer
    float Plus    (float a, float b) { return a+b; }
    float Minus   (float a, float b) { return a-b; }
    float Multiply(float a, float b) { return a*b; }
    float Divide  (float a, float b) { return a/b; }
    
    
    // Solution with a switch-statement - <opCode> specifies which operation to execute
    void Switch(float a, float b, char opCode)
    {
       float result;
    
       // execute operation
       switch(opCode)
       {
          case '+' : result = Plus     (a, b); break;
          case '-' : result = Minus    (a, b); break;
          case '*' : result = Multiply (a, b); break;
          case '/' : result = Divide   (a, b); break;
       }
    
       cout << "Switch: 2+5=" << result << endl;         // display result
    }
    
    
    // Solution with a function pointer - <pt2Func> is a function pointer and points to
    // a function which takes two floats and returns a float. The function pointer
    // "specifies" which operation shall be executed.
    void Switch_With_Function_Pointer(float a, float b, float (*pt2Func)(float, float))
    {
       float result = pt2Func(a, b);    // call using function pointer
    
       cout << "Switch replaced by function pointer: 2-5=";  // display result
       cout << result << endl;
    }
    
    
    // Execute example code
    void Replace_A_Switch()
    {
       cout << endl << "Executing function 'Replace_A_Switch'" << endl;
    
       Switch(2, 5, /* '+' specifies function 'Plus' to be executed */ '+');
       Switch_With_Function_Pointer(2, 5, /* pointer to function 'Minus' */ &Minus);
    }
    I dont find any use of function pointers or even the need for switch statments here. A direct call to the function plus could be made 
    in the function Replace_A_Switch instead of calling Switch. Hence, i could not appreciate the use of function pointers
    in this context. Can anyone explain me better the use of function pointers other than for call back functions ?
    Hoping to get a positive resoponse.
    Thanks & Regards,
    Ani 
     
     
  • 05-23-2008 2:14 PM In reply to

    Re: Use of Function pointers

    I've used them a lot in older C code, but only for callbacks. For a better example, check out the documentation for the C QSort function. A famous place that they're used is the wondows 'WndProc' function that us still used internally in all windows libraries. http://www.toymaker.info/Games/html/wndproc.html

    I'm more likely to use 'functors' or inheritance and virtual methods in C++ or delegates in C#.

    Ralph

     

Page 1 of 1 (2 items)
Seminars           www.Construx.com           Consulting