static functions

I need make something like following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
double CallF1(double (*fun)(double), double y) {
	return (*fun)(y);
}

class A {
	double a;
public:
	static double f1(double x) {return x*x*a;}
	double calc (double x) {
		return CallF1(&f1, x);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	double x = 3;
	A mA;
	
	cout << "x = " << x << endl;
	cout << "CallF1(x) = " << mA.calc(x) << endl;
	return 0;
}


Of course, there are compiler errors because the function CallF1 must have a pointer to the class A. But I don't now how to realise this. Could anybody tell me how to do it?
closed account (EzwRko23)
What are you trying to achieve?
Maybe function objects (functors) are what you need? If so, take a closer look at Boost.

Anyway, I don't recommend this style of programming in C++ - it was not meant to be an object-functional language. Why not make this code purely structural, and then simply pass pointers to functions?
Last edited on
I can not make this code purely structural because that is a small piece of a big project. I only made a simple example to explain what I need. But you must be right. It is not good style of programming.
Topic archived. No new replies allowed.