ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Rep:
C++: Pass function by reference
Is it possible to pass a function or a function handle by reference in c++?
I have the following situation: several functions, let's call them f(t,x), g(t,x) and h(t,x), where t and x are all variables of the same type and length. The only difference between f, g, h is that they give different function values. Now I want to pass f, g, h by reference so that another function can manipulate them without the need to rewrite the function for f, g, h:
void manip(fun,t,x)
{
/* do something with fun at t and x where fun can be f, g, h (or at least some function with the same parameters as f, g or h. */
}
Is that possible or am I thinking too much in terms of MATLAB now?
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Original Poster
Rep:
OK, so if I have a simple (scalar) function that I pass to another function, I could use (CalcX is supposed to be a function that returns a double when given x):
Quote:
typedef double (*PTRFUN)(double);
double somefunction((*f)(double))
{
double value;
x = CalcX(x);
value = (*f)(x) + x;
return value;
}
So, I use "(*f)(x)" to use the value of f at x, which I have initialized in the main function with
Quote:
PTRFUN f;
. The function somefunction for f(x) is called by
Quote:
somefunction(f);
Defining an array of function pointers is easy:
Quote:
PTRFUN fun[n];
where n is some known integer value. I can then call
Quote:
somefunction(f[m]);
if I want to do that for the mth element of the function array.
But is it possible to send the whole array of functions f to somefunction? How would I then call somefunction for f[0] ... f[n-1], and how would I use f and its elements in the function itself: (*f[n])(x) or something more exotic?!
Further, is it possible to use a dynamic array of function pointers? That is, something like:
Quote:
PTRFUN* dynptrfun = NULL;
int n=/* some value which might be determined at runtime */;
dynptrfun = new PTRFUN[n];
/* initialize elements of dynamic array */
/* do something with it */
delete [] dynptrfun;
dynptrfun = NULL;
But is it possible to send the whole array of functions f to somefunction? How would I then call somefunction for f[0] ... f[n-1], and how would I use f and its elements in the function itself: (*f[n])(x)
That looks right to me.
Quote:
Further, is it possible to use a dynamic array of function pointers? That is, something like:
You seem to already understand it all. You could confirm you are correct more quickly by trying it than by asking a question here.
You also seem to understand the idea of using a typedef to help, but I'll stress that idea.
As you get into complicated types (such as an array of pointers for functions with a specific signature) C and C++ have general methods for cramming the whole mess into one type specification, but it is a bad idea to use them. Instead use multiple typedefs, each one adding just one layer to what the previous defined. That makes it much easier to understand the error messages when you get something wrong and it makes it much easier to read the code later and understand what you did.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.