Function Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kapil Khosla

    Function Pointers

    Hi,
    I am trying to understand the use of Function Pointers. I wrote a
    small to understand the use but am not convinced that it is the
    correct implementation.

    Can you show me a way to remove the switch statement in this case ?
    Thanks,
    Kapil

    #include "stdafx.h"
    using namespace std;

    float Plus(float a, float b) {return a + b; }
    float Minus(float a, float b) {return a - b; }
    float Mult(float a, float b) {return a * b; }
    float Div(float a, float b) { return a / b; }
    float Func(float,floa t);

    void Switch_With_Fun ction_Pointer(f loat,float,floa t
    (*pt2Func)(floa t,float));
    int main()
    {

    float a,b;
    float (*Func)(float,f loat);
    char opCode;
    cout << "Enter the numbers and operation" << endl;
    cin >> a >> b >> opCode;
    // Do I really need this switch ,is there a faster way !!
    switch(opCode)
    {
    case '+' : Func = Plus;
    break;
    case '-' : Func = Minus;
    break;
    case '*' : Func = Mult;
    break;
    case '/' : Func = Div;
    break;
    }

    Switch_With_Fun ction_Pointer(a ,b,Func);
    return 0;
    }

    void Switch_With_Fun ction_Pointer(f loat a,float b,float
    (*pt2Func)(floa t a,float b))
    {
    float result = pt2Func(a,b);
    cout << "Switch with function result is " << result << endl;
    }
  • Victor Bazarov

    #2
    Re: Function Pointers

    "Kapil Khosla" <khoslakapil@ya hoo.com> wrote...[color=blue]
    > I am trying to understand the use of Function Pointers. I wrote a
    > small to understand the use but am not convinced that it is the
    > correct implementation.[/color]

    Your implementation is fine.
    [color=blue]
    > Can you show me a way to remove the switch statement in this case ?[/color]

    Put the function pointers into a map<char,double (*)(double,doub le)>
    and extract the required pointer using the opCode as the Key.
    [color=blue]
    > Thanks,
    > Kapil
    >
    > #include "stdafx.h"
    > using namespace std;
    >
    > float Plus(float a, float b) {return a + b; }
    > float Minus(float a, float b) {return a - b; }
    > float Mult(float a, float b) {return a * b; }
    > float Div(float a, float b) { return a / b; }
    > float Func(float,floa t);
    >
    > void Switch_With_Fun ction_Pointer(f loat,float,floa t
    > (*pt2Func)(floa t,float));
    > int main()
    > {
    >
    > float a,b;
    > float (*Func)(float,f loat);
    > char opCode;
    > cout << "Enter the numbers and operation" << endl;
    > cin >> a >> b >> opCode;
    > // Do I really need this switch ,is there a faster way !!
    > switch(opCode)
    > {
    > case '+' : Func = Plus;
    > break;
    > case '-' : Func = Minus;
    > break;
    > case '*' : Func = Mult;
    > break;
    > case '/' : Func = Div;
    > break;
    > }
    >
    > Switch_With_Fun ction_Pointer(a ,b,Func);
    > return 0;
    > }
    >
    > void Switch_With_Fun ction_Pointer(f loat a,float b,float
    > (*pt2Func)(floa t a,float b))
    > {
    > float result = pt2Func(a,b);
    > cout << "Switch with function result is " << result << endl;
    > }[/color]


    Comment

    Working...