views:

238

answers:

7

The following uses a simple function pointer, but what if I want to store that function pointer? In that case, what would the variable declaration look like?

#include <iostream>
#include <vector>

using namespace std;

double operation(double (*functocall)(double), double wsum);
double get_unipolar(double);
double get_bipolar(double);

int main()
{
    double k = operation(get_bipolar, 2); // how to store get_bipolar?
    cout << k;
    return 0;
}
double operation(double (*functocall)(double), double wsum)
{
    double g = (*functocall)(wsum);
    return g;
}
double get_unipolar(double wsum)
{
    double threshold = 3;
    if (wsum > threshold)
        return threshold;
    else
        return threshold;
}
double get_bipolar(double wsum)
{
    double threshold = 4;
    if (wsum > threshold)
        return threshold;
    else
        return threshold;
}
+5  A: 

You already (almost) have it in your code:

double (*functocall)(double) = &get_bipolar;

This defines a function pointer named functocall which points to get_bipolar.

Péter Török
roe
like this ??double (*functocall)(double) = k=operation(???,2); // then what to put here ??
ismail marmoush
Manuel
I think the ampersand only worked in C (and still was optional), right?
fortran
@Manuel: are you sure about that, it feels like functions are by their very definition pointers (to code) already.
roe
i tested it it's not working
ismail marmoush
@roe - Yes, I'm completely sure. @fortran - C++ has C compatibility
Manuel
@ismail - Maybe you should put angle brackets in those #includes ?
Manuel
@ manuelnope i intentionally did it this way cause of the website <pre> tags but i think i wrote it in wrong place i'm sorry :) thanks alot this website and you people are amazing really !!
ismail marmoush
+1  A: 
typedef double (*func_t)(double); 
func_t to_be_used = get_bipolar
pierr
A: 
double (*foo)(double);

where foo is the variable name.

Vebjorn Ljosa
+2  A: 
typedef double (*PtrFunc)(double); 

PtrFunc ptrBipolar = get_bipolar;


OR


typedef double (Func)(double); 

Func *ptrBipolar = get_bipolar;

which ever you are comfortable to use.

Ashish
Though when typedefing function types, I'd name them "Func" instead of "PtrFunc".
Roger Pate
yes definately ,you are right. I am correcting it.
Ashish
A: 

You should consider using a typedef:

 typedef double (*MyFunc)(double);
 MyFunc ptr_func = &get_bipolar;
 (*ptr_func)(0.0);


double operation(MyFunc functocall, double wsum)
{
    double g;
    g = (*functocall)(wsum);
    return g;
}

May I recommend also the identity template trick:

template<class T>
class Id
{
    typedef T type;
};

Id<double(double)>::type * ptr_func = &get_bipolar;
MyFunc func = &get_bipolar;
(*ptr_func)(0.0);


double operation(Id<double(double)>::type * functocall, double wsum)
{
    double g;
    g = (*functocall)(wsum);
    return g;
}
Manuel
+2  A: 

You code is almost done already, you just seem to call it improperly, it should be simply

double operation(double (*functocall)(double), double wsum)
{
    double g;
    g = functocall(wsum);
    return g;
}

If you want to have a variable, it's declared in the same way

double (*functocall2)(double) = get_bipolar;

or when already declared

functocall2 = get_bipolar;

gives you a variable called functocall2 which is referencing get_bipolar, calling it by simply doing

functocall2(mydouble);

or passing it to operation by

operation(functocall2, wsum);
roe
yesssss that's what i wanted .. exactly i wanted to save the get_bipolar in a variable .. so i can use that variable again in some other place or even make an array of it
ismail marmoush
i declared the functocall2 at the header file , how the assign would look like in a cpp file ????????????
ismail marmoush
Simply `functocall2 = get_bipolar;` just like any other assignment.
MSalters
+1  A: 

Have a look at boost function, it's a header only library that tidies things up a little (IMHO): http://www.boost.org/doc/libs/1_42_0/doc/html/function.html

typedef boost::function<double (double)> func_t;
func_t to_be_used = &get_bipolar;

(NB: different syntax required for VC6)

Patrick
In the OP's case this will add some overhead to every call without any additional benefits
Manuel
There are advantages and disadvantages to boost function. The overhead is likely to be insignificant in the OP's use case, the additional benefit is in tidier code and greater extendibility. Seemed worth mentioning in a forum like this so readers can see what's available.
Patrick