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;
} 1 4933
"Kapil Khosla" <kh*********@yahoo.com> 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; } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Markus Dehmann |
last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have
millions of inserts and many more lookups, and my profiler found that they
cost a lot of runtime.
Therefore, I want to...
|
by: Sweety |
last post by:
hi,
Is main function address is 657.
its show in all compiler.
try it & say why?
bye,
|
by: Dennis Chang |
last post by:
Hi all,
I was reading about function pointers and came across something which
intrigued me.
K&R2 calls qsort (pg.119) within main as so:
qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
|
by: Alexei A. Frounze |
last post by:
Seems like, to make sure that a pointer doesn't point to an object/function,
NULL (or simply 0) is good enough for both kind of pointers, data pointers
and function pointers as per 6.3.2.3:
3 An...
|
by: Bill Pursell |
last post by:
The following code generates a compiler warning
when compiled with gcc -pedantic:
typedef (*FUNC)(int);
FUNC f;
void *
get_f(void)
{
return &f;
|
by: Robert Seacord |
last post by:
i am trying to print the address of a function without getting a
compiler warning (i am compiling with gcc with alot of flags).
if i try this:
printf("%p", f);
i get:
warning: format %p...
|
by: John |
last post by:
Is the following program print the address of the function?
void hello()
{ printf("hello\n");
}
void main()
{
printf("hello function=%d\n", hello);
}
|
by: Christian Maier |
last post by:
Hi
After surfing a while I have still trouble with this array thing. I
have the following function and recive a Segmentation fault, how must
I code this right??
Thanks
Christian Maier
|
by: Josefo |
last post by:
Hello, is someone so kind to tell me why I am getting the following
errors ?
vector_static_function.c:20: error: expected constructor, destructor,
or type conversion before '.' token...
|
by: copx |
last post by:
Why doesn't the C standard include generic function pointers?
I use function pointers a lot and the lack of generic ones is not so cool.
There is a common compiler extension (supported by GCC...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |