leaf wrote:
consider these functions:
void foo()
{
cout << "foo() called;
}
void sayhello( std::string& msg ){
msg = "something";
}
int add( int x, int y)
{
return x + y;
}
typedef void (*fooFunc)();
typedef std::map<std::string, fooFunc> Processing_Map;
..
Processing_Map table;
table["foo"] = foo;
table["sayhello"] = ???
table["add"]= ???
how to map table["sayhello"] to sayhello function, considering it has
a parameter
also the "add"
and how to actually pass a parameter value to those mapped functions
function pointers are typed. You can't.
One way of doing it would be to wrap foo:
foo(std::string& msg) {
foo();
}
change your typedef:
typdef void (*fooFunc)(std::string&);
That only provides you with limited ability to call heterogeneous
function prototypes.
A typical "solution" is:
typedef void* (*fooFunc)(void*, void*);
Which allows you to return and accept arbitrary data by completely
removing type safety.
The difficulty is in keeping track of what to pass to which function.
If you know at the call site which type of data to pass, then just call
the correct function (or at least a function through a typesafe function
pointer).
Ben Pope
--
I'm not just a number. To many, I'm known as a string...