*
:
> Essentially what I am trying to do is copy the idea of a "foldr"
> function or something like a "map" function, which takes a function as
> a parameter and then applies to recursively/iteratively to to some data
> structure.
>
> SO, for example, if I defined the function
>
> double add2(double d) {
> return d+2.0;
> }
>
> and then I would like to do the following: map(add2) listofDoubles;
> where listOfDoubles is an array of doubles.
#include <algorithm> // std::copy, std::transform
#include <iterator> // std:

stream_iterator
#include <iostream> // std:.cout
#include <ostream> // <<, std::endl
#include <vector> // std::vector
void display( std::vector<int> const& v )
{
std::copy(
v.begin(), v.end(),
std:

stream_iterator<int>( std::cout, " " )
);
std::cout << std::endl;
}
int add2( int x ) { return x+2; }
int main()
{
static int const data[] = {1, 2, 3};
std::vector<int> v( data, data + 3 );
display( v );
std::transform( v.begin(), v.end(), v.begin(), add2 );
// std::transform is the C++ "apply".
display( v );
}
You know, this is rather interesting. For when I replace 'int' with
'double' my trusty old Microsoft Visual C++ 7.1 compiler says:
fatal error C1001: INTERNAL COMPILER ERROR (compiler file
'f:\vs70builds\3077\vc\Compiler\Utc\src\P2\main.c' , line 14
And guess what, it's the old 'int main' issue raring its ugly head.
For my editor of course changed that to 'double main' -- so now I know
the two thousand and forty third way of ICEing this compiler...
> Anyway, I'm not really after a map,
Any function is a map, isn't it?
> but the same principle applies. I
> want to take a function as a parameter and I want want to work through
> a kind of tree applying the function supplied to each node in the tree.
You can use a function pointer or an object with a virtual member
function or a functor object, but don't use member function pointers.
E.g.,
typedef double FuncFoo( double x );
void apply( Node& node, FuncFoo foo )
{
node.data = foo( node.data );
apply( *node.left, foo );
apply( *node.right, foo );
}
> Now, the idea is that there are several classes I want to implement,
> each of which can list a number of functions.
>
> For example, class A might have 12 functions it needs to apply, class B
> has 2, class C might have 4 and so forth. So, what I was hoping is that
> A, B, C could all have base class X, where X basically just contained a
> function pointer, and then, inside A, for example, I could get the
> function pointer (inherited from X) to point to a different function
> defined in A whenever I wanted to.
?
I think you'd better forget that idea for a solution. Don't think in
terms of changing function pointers. Just supply the relevant functions
or objects or whatever as arguments to your 'apply' function.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?