471,851 Members | 836 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,851 software developers and data experts.

"Local function"?

Hi all,

Suppose I have a routine to find the maximum
of any *univariate* function f in the interval [a, b]:

double f(double x);
double findmax(double (*func)(double), double a, double b);

Now I have a function with three variables:

double g(double x, double y, double z);

And I want to find the maximum of g, with y=y0 and z=z0 fixed, and
x in the interval [a, b].

How can I achieve this? Since C++ does not allow local functions,
the only thing I can think of right now is to modify the findmax
function:

double findmax(double (*func)(double, double, double),
double y, double z, double a, double b);

However, this means if someday I have another function h with four variables,
I'll have to modify findmax again. How can I have a universal findmax?

Thanks for your help.

jack
Jul 19 '05 #1
1 3803
"jack" <id***@yahoo.com> wrote...
Suppose I have a routine to find the maximum
of any *univariate* function f in the interval [a, b]:

double f(double x);
double findmax(double (*func)(double), double a, double b);

Now I have a function with three variables:

double g(double x, double y, double z);

And I want to find the maximum of g, with y=y0 and z=z0 fixed, and
x in the interval [a, b].

How can I achieve this? Since C++ does not allow local functions,
It doesn't allow stand-alone local functions, but you can have
a local type with an inline member function, which will act just
like a local function.
the only thing I can think of right now is to modify the findmax
function:

double findmax(double (*func)(double, double, double),
double y, double z, double a, double b);

However, this means if someday I have another function h with four variables, I'll have to modify findmax again. How can I have a universal findmax?


You need to define your 'findmax' not in terms of a function pointer
but in terms of a functor. Try this

template<class T, class F>
T findmax(F func, T a, T b)
{
.. // do what you do, but replace 'double' with T if needed
}

That will allow you to use 'bind2nd' and 'bind1st' function adapters
with which you could "fix" some arguments and let others do the job.

Read more on templates and functors and adapters in your favourite
C++ book.

Victor
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tom Clement | last post: by
3 posts views Thread by Zeljko | last post: by
3 posts views Thread by Robin Tucker | last post: by
1 post views Thread by princessjewel62 | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.