473,238 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 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 3904
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tom Clement | last post by:
Hi folks. I have a question about "Copy Local". We have quite a few projects in our solution (35) and some of them reference the very large "Microsoft.mshtml.dll". I was wondering why leave...
3
by: Doc | last post by:
Hi all, Hoping someone can make me smart. Have SQL Srv2000 database linked (ODBC) for query. Have my completed Query. Want to save out THAT data to a local area for later manipulation (in Access...
3
by: Zeljko | last post by:
Hi, Is there a way to define a local variable that retains it's value between the function calls ? Zeljko
3
by: Robin Tucker | last post by:
I'm fetching a list of SQL servers on a domain using a function that returns strings, including "(local)" for the local machine (if it has an SQL server on it). Now, presumably in a different...
3
by: Robin Tucker | last post by:
Should I consider this to work in other locales? If theServer.Name <> "(local)" and theServer.Name <> "(localhost)" Then ' Can only use local machine in this version..... End If
6
by: murgan | last post by:
Hi people, i am new to this group,this is my first query, friends i want to know the difference between "function pointer" and "pointer to a function" in c lang, so friends please send the...
1
by: princessjewel62 | last post by:
I would like to ask anyone out there, where can I find the "Local Internet Zone" as I had a problem with my application not displaying and saying that I have no authorisation to view this web page...
4
by: soren625 | last post by:
What I am trying to do is grab a little snippet of data from a remote page based on user input in a form. Take a look at this page: http://www.qrz.com/kb2gsd What I want to do is: when a user...
2
by: kirk | last post by:
I'd like to programmatically be able to determine if a network interface is currently in "Local Only" or "Local and Internet" access mode. Also I wasn't sure if this is only applicable to Windows...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.