Article View: comp.std.c++
Article #667NAMELESS FUNCTIONS
From: John Stephens
Date: Wed, 11 Nov 1992 18:59
Date: Wed, 11 Nov 1992 18:59
44 lines
2327 bytes
2327 bytes
I'm trying to write a function that accepts a one variable math function
(double argument, double return type) and returns another math function
which will calculate the derivative of the original function argument.
In other words, the following will be possible:
double (*dsqrt)(double) = derivative(sqrt);
cout << dsqrt(x);
The main problem I am running into is that it seems to be impossible for
the returned function to know what function calculate the derivative
for because it only accepts one argument, and the derivative function
has no way of setting the function inside the calculating function!
(Does anyone understand what I'm trying to say?)
I was thinking of somehow doing it with classes, which would be much
easier. The usage would be as follows:
Function f(sqrt);
cout << f.derivative(10);
Or, of course, I could write a general derivative function that accepts
a one variable math function and a double value and just return the
derivative at that point. But I would still like to be able to do it the
way I stated originally. One thing that would solve the problem would be
a modified version of C/C++ that would allow the following:
double (*derivative(double (*f)(double)))(double)
{
double derivCalc(double x)
{
...calculate and return the derivative of f(x)...
}
return derivCalc;
}
It would allow functions to be defined within other functions and use
all of their local variables, without having them explicitly entered as
arguments. In other words, each derivCalc returned could access a
different version of f. Unfortunately I think this is impossible in C
and C++, but who knows.
John Stephens
john.stephens@uttsbbs.uucp
Technical Details
Message-ID:
<667.22.uupcb@uttsbbs.uucp>Path:
arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!caen!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!boo!uttsbbs!john.stephensShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!caen!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!boo!uttsbbs!john.stephens From: john.stephens@uttsbbs.uucp (John Stephens) Newsgroups: comp.std.c++ Subject: NAMELESS FUNCTIONS Message-ID: <667.22.uupcb@uttsbbs.uucp> Date: 11 Nov 92 18:59:00 GMT Distribution: world Organization: The Transfer Station BBS, Danville, CA - 510-837-4610/837-5591 Reply-To: john.stephens@uttsbbs.uucp (John Stephens) Lines: 44