LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2008, 03:14 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
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?
 
Old 11-12-2008, 03:35 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
you can certainly do it, how you do it will depend if the function is a non static member function or not.

You first need to get the function type determined:
non-member function
int (*)(int,float)
member function
int (MyClass::*)(int,float)
 
Old 11-12-2008, 04:26 AM   #3
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
They are called function pointers, quite logically... as if I could not think of that before.

Last edited by unihiekka; 11-12-2008 at 11:20 AM.
 
Old 11-30-2008, 10:30 AM   #4
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
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;

Last edited by unihiekka; 11-30-2008 at 10:34 AM.
 
Old 11-30-2008, 10:48 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by unihiekka View Post
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.
 
Old 11-30-2008, 01:35 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by unihiekka View Post
They are called function pointers, quite logically... as if I could not think of that before.
Well, you can also pass them as references if you use the right type specification:
Code:
void function();
//...
void(&reference)() = function;
reference();
That isn't useful unless you won't need to change what it points to, and it obviously can't be used in an array.
ta0kira

Last edited by ta0kira; 11-30-2008 at 01:38 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pass by reference in C xeon123 Programming 4 10-22-2007 07:32 PM
How do I pass a C++ object reference to GTK+ callbacks??? paulsm4 Programming 1 12-14-2005 03:32 PM
how pass a class to a function aditya1 Programming 2 03-08-2005 09:02 PM
Undefined reference to function error Quest101 Linux - Newbie 0 12-30-2004 05:01 PM
function, pass by reference question true_atlantis Programming 5 04-02-2004 11:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration