Velocity Reviews > C++ > Function Pointers

# Function Pointers

Kapil Khosla
Guest
Posts: n/a

 07-18-2003
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,float);

void Switch_With_Function_Pointer(float,float,float
(*pt2Func)(float,float));
int main()
{

float a,b;
float (*Func)(float,float);
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_Function_Pointer(a,b,Func);
return 0;
}

void Switch_With_Function_Pointer(float a,float b,float
(*pt2Func)(float a,float b))
{
float result = pt2Func(a,b);
cout << "Switch with function result is " << result << endl;
}

Victor Bazarov
Guest
Posts: n/a

 07-18-2003
"Kapil Khosla" <(E-Mail Removed)> wrote...
> 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.

Your implementation is fine.

> Can you show me a way to remove the switch statement in this case ?

Put the function pointers into a map<char,double(*)(double,double)>
and extract the required pointer using the opCode as the Key.

> 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,float);
>
> void Switch_With_Function_Pointer(float,float,float
> (*pt2Func)(float,float));
> int main()
> {
>
> float a,b;
> float (*Func)(float,float);
> 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_Function_Pointer(a,b,Func);
> return 0;
> }
>
> void Switch_With_Function_Pointer(float a,float b,float
> (*pt2Func)(float a,float b))
> {
> float result = pt2Func(a,b);
> cout << "Switch with function result is " << result << endl;
> }

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post cerr C Programming 12 04-07-2011 11:17 PM n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM Peter Goddard C Programming 3 05-16-2005 09:44 PM komal C++ 6 01-25-2005 11:13 AM Phil C++ 1 09-16-2003 02:17 AM

Advertisments