Can i give a function as a parameter?

Hi!

I have a quiet simple question. I have a class, which represents curves, it has an Xaxis vector and an Yaxis vector. I would like to make a constructor for this class, which gets a function as a parameter, and "builds" the curve.ű

For example, like this way:

Curve a(sin);

and my constructor would look like:

Curve::Curve(--the part what i need to know--, size_t xsize)
{
for(int i=0;i<xsize;i++)
{
Xaxis.push_back(i);
Yaxis.push_back(--the function given in the parameter 1---(i));
}
}


I hope there is a way to implement that. Thanks for the help in advance.

Balazs
In this case, you want to pass in trig functions like:
1
2
double cos(double);
double sin(double);
and so on. So you can declare a function type like this:
 
typedef double trigfunc(double);

Then you can define you constructor as:
1
2
3
4
5
6
7
8
Curve::Curve(trigfunc* f, size_t xsize)
{
    for (int i = 0; i < xsize; ++i)
    {
        Xaxis.push_back(i);
        Yaxis.push_back(f(i));
    }
}
Thanks a lot, it works.
Topic archived. No new replies allowed.