On Mar 31, 3:16 pm, Andrey Bulat <andrey.bu...@gmail.com> wrote:
On Mar 31, 8:50 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
Vladyslav Lazarenko wrote:
On Mar 31, 12:42 pm, oalfishcivil <yuefei...@gmail.com> wrote:
I have a function double solver(double (*pf)(double), double to=
different arguments and different results. In general case there's no
way to replace all these invocations with a single invocation in
advance. If that were possible, there probably wouldn't be a callback
there in the first place.
--
Best regards,
Andrey Tarasevich
I can propose for discussion following simple sample:
class Functor2
{
public:
Functor2(int A, int B): a(A),b(B){}
int a;
int b;
virtual int operator()()
{
return b;
}
};
class Functor1: public Functor2
{
public:
Functor1(int A): Functor2(A, 0){}
virtual int operator()()
{
return a;
}
};
int solver(Functor2* p, int d)
{
return (*p)() + d;
}
int main()
{
Functor1 f1(5);
Functor2 f2(3,2);
int ret;
ret = solver(&f1, 7);
ret = solver(&f2, 7);
}
As for me it is more valuable construction versus global variable (and
much more readeble).
(It works on VS2008)
Best wishes
Andrey Bulat
Really appreciate your example and others' suggestions. I am writing a
code use bisection method to find roots in given bounds for
functions like double func(double x) or double func(double x,double
para). Since I have several different such functions, I want to write
a bisection method function which can take this functions as argument
instead of adding the bisection method in each function which will
produce a lot more repetitive codes. I think your method here would
work for me although it's not that elegant(not your fault). Thanks
Yuefei