Function pointers by example
Author |
Message |
Martin
|
Posted: Thu Jun 08, 2006 3:14 am Post subject: Function pointers by example |
|
|
Function pointers are a powerful feature of C++.
code: | #include <iostream>
using namespace std;
int do_it(int x, int y, int (*func)(int,int))
{
return func(x,y);
}
int add(int x, int y)
{
return x + y;
}
int subt(int x, int y)
{
return x - y;
}
int mult(int x, int y)
{
return x * y;
}
int main()
{
int a, b, c = 1;
while(c!=0)
{
cout << "Hello. Would you like to\n0)Quit\n1) Add\n2) Subtract or\n3) Multiply\ntoday?" << endl;
cin >> c;
cout << "Give me a and b please" << endl;
cin >> a >> b;
if(c==1)
cout << a << " + " << b << " = " << do_it(a,b,add);
if(c==2)
cout << a << " - " << b << " = " << do_it(a,b,subt);
if(c==3)
cout << a << " * " << b << " = " << do_it(a,b,mult);
cout << endl << endl;
}
cout << "Au revoir!" << endl;
return 0;
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Thu Jun 08, 2006 9:12 am Post subject: (No subject) |
|
|
Actually. function pointers are a feature of C. C++ has an even better way to do this. Templates, type inference, and function objects.
code: | #include <iostream>
#include <functional>
using namespace std;
template <typename T, typename F>
T do_it(T x, T y, F func)
{
return func(x, y);
}
int main()
{
int a, b, c = 1;
while (c != 0)
{
cout << "Hello. Would you like to"<< endl
<< "0)Quit" << endl
<< "1) Add" << endl
<< "2) Subtract or" << endl
<< "3) Multiply" << endl
<< "today?" << endl;
cin >> c;
cout << "Give me a and b please" << endl;
cin >> a >> b;
switch (c)
{
case 1:
cout << a << " + " << b << " = " << do_it(a, b, plus<int>());
break;
case 2:
cout << a << " - " << b << " = " << do_it(a, b, minus<int>());
break;
case 3:
cout << a << " * " << b << " = " << do_it(a, b, multiplies<int>());
break;
}
cout << endl << endl;
}
cout << "Au revoir!" << endl;
return 0;
} |
|
|
|
|
|
|
avok23
|
Posted: Sun May 04, 2008 3:49 am Post subject: RE:Function pointers by example |
|
|
i have been using c++ for a good while and still havent found a reason to use this.
i hear its usefull in ai. |
|
|
|
|
|
wtd
|
Posted: Sun May 04, 2008 11:20 am Post subject: RE:Function pointers by example |
|
|
Think about callbacks. |
|
|
|
|
|
Saad
|
Posted: Sun May 04, 2008 11:24 am Post subject: (No subject) |
|
|
wtd @ Thu Jun 08, 2006 9:12 am wrote: Actually. function pointers are a feature of C. C++ has an even better way to do this. Templates, type inference, and function objects.
I never knew about this. This way seems much better and easier then funtion pointers. Thanks! |
|
|
|
|
|
Narlayusin
|
Posted: Wed Mar 22, 2017 4:27 am Post subject: RE:Function pointers by example |
|
|
It is the information that should be told to everyone really. |
|
|
|
|
|
ThomasWown
|
Posted: Sat Mar 25, 2017 1:11 am Post subject: RE:Function pointers by example |
|
|
@Martin - 100% Agreed! This is useful because functions encapsulate behavior. For instance, every time you need a particular behavior such as drawing a line, instead of writing out a bunch of code, all you need to do is call the function. |
|
|
|
|
|
|
|