help using function pointer at run-rime

  • Thread starter Jean-Daniel Gamache
  • Start date
J

Jean-Daniel Gamache

Hi, I want to be able to call a function specified at run-time (in a string).
How can I do that in C++ ????

ie:



char myFunctionName[] = "myFunction";

void myFunction () {
}


//this is the tricky part i am wondering how i could call
// a function that is specified in my variable myFunctionName?

functionPtr = GetFunctionAddress(myFunctionName);
functionPtr();





thanks,
JD
 
A

Alf P. Steinbach

I want to be able to call a function specified at run-time (in a string).
How can I do that in C++ ????

C++ provides no direct facility for that.

To do it using only standard C++ you'd have to make a table of all relevant
functions, or generate code and call the compiler from your program.

However, what is it that you're trying to achieve by doing that?
 
M

Mike Wahler

Jean-Daniel Gamache said:
Hi, I want to be able to call a function specified at run-time (in a string).
How can I do that in C++ ????

You cannot. Once a C++ program is compiled, there
are no function 'names' only memory addresses.
ie:



char myFunctionName[] = "myFunction";

This is just an array of characters containing a
C style string. It has no connection with any
function, unless you associate it with some
function's address (e.g. with a std::map)
void myFunction () {
}


//this is the tricky part i am wondering how i could call
// a function that is specified in my variable myFunctionName?

Change your variable's type to a pointer to a function,
and assign or initialize it with the address of the desired function.

void (*f)() = myFunction;

Call it like this:

f();
functionPtr = GetFunctionAddress(myFunctionName);
functionPtr();

This could be implemented e.g.

std::map<std::string, void (*)()> table;
table["myFunction"] = myFunction;

table["myFunction"](); /* function call */

Of course you'll need to add all the 'scaffolding'
e.g. put this stuff into functions, provide the
needed header(s) etc.

-Mike
 
G

Gianni Mariani

Jean-Daniel Gamache said:
Hi, I want to be able to call a function specified at run-time (in a string).
How can I do that in C++ ????

ie:



char myFunctionName[] = "myFunction";

void myFunction () {
}


//this is the tricky part i am wondering how i could call
// a function that is specified in my variable myFunctionName?

functionPtr = GetFunctionAddress(myFunctionName);
functionPtr();

The only portable way to do this is to place these in a registry.

This whole discussion very quickly leads to factories.

Let's start with somthig along the lines of what you did.

#include <cstring>

char myFunction1Name[] = "myFunction1";

void myFunction1 () {
}


char myFunction2Name[] = "myFunction2";

void myFunction2 () {
}

struct RegistryEntry {
const char * name;
void ( * func )();
};

RegistryEntry Registry[] =
{
{ myFunction1Name, & myFunction1 },
{ myFunction2Name, & myFunction2 },
};

int RegistryCount = sizeof( Registry ) / sizeof( * Registry );

void ( * GetFunctionAddress( const char * name ) )()
{
for ( int i = RegistryCount - 1; i >= 0; i -- )
{
if ( std::strcmp( name, Registry.name ) == 0 )
{
return Registry.func;
}
}

return 0;
}

OK - Problem number 1 - only functions that you compile into the
"Registry" are accessible.

Most reasons for doing this is so that they can load modules at a later
point in time and this system requires a recompilation of the array
"Registry".

The second thing, often you need some context and a collection of method
- oops, this is called a class.

The third thing, you'll want it to be able to do this with dynamically
loaded objects (.dll's or .so's).

The fourth thing, you'll want to be able to reliably unregister them as
well.

Anything else ? Oh yeah, network transparency - starting to look like
COM and Corba.

I have one of these factory frameworks which is a bunch of templates
that does most of this. It's a general purpose system. Let me know if
you're interested but it's far more involved than the system above.

G
 
Y

Yamin

Jean-Daniel Gamache said:
Hi, I want to be able to call a function specified at run-time (in a string).
How can I do that in C++ ????

ie:



char myFunctionName[] = "myFunction";

void myFunction () {
}


//this is the tricky part i am wondering how i could call
// a function that is specified in my variable myFunctionName?

functionPtr = GetFunctionAddress(myFunctionName);
functionPtr();





thanks,
JD

Hey Jean,

Someone on the newsgroup had a similar situation recently. He had a nice
way to handle it using std::map.
Use string names "myfunction" as the key and a function pointer as the
value.

Insert each key/value pair once on startup, then to call a function, just
use the map.

Yamin
 
M

Mike Wahler

Yamin said:
Jean-Daniel Gamache said:
Hi, I want to be able to call a function specified at run-time (in a string).
How can I do that in C++ ????

ie:



char myFunctionName[] = "myFunction";

void myFunction () {
}


//this is the tricky part i am wondering how i could call
// a function that is specified in my variable myFunctionName?

functionPtr = GetFunctionAddress(myFunctionName);
functionPtr();





thanks,
JD

Hey Jean,

Someone on the newsgroup had a similar situation recently. He had a nice
way to handle it using std::map.
Use string names "myfunction" as the key and a function pointer as the
value.

Insert each key/value pair once on startup, then to call a function, just
use the map.

This is exactly what I showed in my reply.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top