Function pointers are one of those odd elements of C++ that don't always have an obvious application. Since I am of a math background, I'm going to look at some ways that function pointers can be useful. One of the peculiar properties of math is that, when you advance far enough, you start looking at functions as things to be worked on. Some practical examples include:
Composition of functions: f(g(h(x)))
Derivatives of functions
Integrals of functions
Arrays of functions
In physics, there are also a variety of applications of the above, plus various other applications of principles where the exact function is not known in advance. One of the basic ideas that appears in physics is the concept of an average rate of change. The basic idea is this: average velocity is the average rate of change of position as a function of time: (pos(final)-pos(start))/(final-start). Average acceleration is the average rate of change of velocity (same formula, use vel instead of pos). Calculus is obsessed with this concept.
In C++, we can use a pointer to a function to implement this concept with a single function. You declare a pointer to a function like this:
An example of a program using this concept is avevel.cpp:Code:double f(double); //function that takes a double and returns a double double (*fptr)(double); //pointer to a function that takes a double and returns a double double *g(double); //function that takes a double and returns a pointer to a double fptr = &f //valid g = &f //error, g is not a pointer fptr = &g //error g and fptr return different types
The output is:Code:#include <iostream> #include <cmath> const double pi = 3.14159265358979323846264338327950288419716939937510; typedef double(* FCT_PNTR)(double); double avechange(FCT_PNTR f, double start,double end) { if (start == end) return 0.0; else return (f(end) - f(start))/(end - start); } double square(double x) { return x*x; } double cube(double x) { return x*x*x; } int main() { std::cout<<"sin from 0 to pi: "<<avechange(&sin,0.0,pi)<<"\n"; std::cout<<"cos from 0 to pi: "<<avechange(&cos,0.0,pi)<<"\n"; std::cout<<"square from 0 to 2: "<<avechange(&square,0.0,2.0)<<"\n"; std::cout<<"square from 2 to 0: "<<avechange(&square,2.0,0.0)<<"\n"; std::cout<<"square from -2 to 0: "<<avechange(&square,-2.0,0.0)<<"\n"; std::cout<<"square from 0 to -2: "<<avechange(&square,0.0,-2.0)<<"\n"; std::cout<<"square from -2 to 2: "<<avechange(&square,-2.0,2.0)<<"\n"; std::cout<<"square from 0 to 0: "<<avechange(&square,0.0,0.0)<<"\n"; std::cout<<"cube from 0 to 2: "<<avechange(&cube,0.0,2.0)<<"\n"; std::cout<<"cube from 2 to 0: "<<avechange(&cube,2.0,0.0)<<"\n"; std::cout<<"cube from -2 to 0: "<<avechange(&cube,-2.0,0.0)<<"\n"; std::cout<<"cube from 0 to -2: "<<avechange(&cube,0.0,-2.0)<<"\n"; std::cout<<"cube from -2 to 2: "<<avechange(&cube,-2.0,2.0)<<"\n"; std::cout<<"cube from 0 to 0: "<<avechange(&cube,0.0,0.0)<<"\n"; }
Code:sin from 0 to pi: 3.89804e-17 cos from 0 to pi: -0.63662 square from 0 to 2: 2 square from 2 to 0: 2 square from -2 to 0: -2 square from 0 to -2: -2 square from -2 to 2: 0 square from 0 to 0: 0 cube from 0 to 2: 4 cube from 2 to 0: 4 cube from -2 to 0: 4 cube from 0 to -2: 4 cube from -2 to 2: 4 cube from 0 to 0: 0
Wow! I am just starting to learn C++, haven't even really started the programming part of it but I know some things. Maybe this might make more sense to me when actually I understand more C++.
The thing I don't understand is the output? How do they work out?
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
std::cout is the output stream. << sends the following object into the output stream, and the << operator can be chained. Finally, avechange has a return value that << knows how to pass to the output stream, so life is good![]()
Oh, I sort of understand that partThanks Winged, I knew that std::cout was the output system and I know << puts it to the output system
![]()
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
Looking at one line:
std::cout<<"sin from 0 to pi: " pushes the string into the output stream and returns std::cout, this brings the output to "sin from 0 to pi: " and the code to be executed toCode:std::cout<<"sin from 0 to pi: "<<avechange(&sin,0.0,pi)<<"\n";
std::cout<<avechange(&sin,0.0,pi) calls avechange which returns a value that is approximate 0, pushes that value as a string into the output stream and returns std::cout, this brings the output to "sin from 0 to pi: 3.89804e-17Code:std::cout<<avechange(&sin,0.0,pi)<<"\n";
" and the code to be executed to
std::cout<<"\n" adds a newline to the output and returns std::cout, which brings the code to be executed to:Code:std::cout<<"\n";
This evaluates to something (nobody really cares) and terminates the statement.Code:std::cout;
Oh that makes much more sense nowThanks Winged, I will start to read the book now. Done most of my browsing for the day.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks