C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help using function pointer at run-rime

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jean-Daniel Gamache
Guest





PostPosted: Mon Sep 29, 2003 9:30 pm    Post subject: help using function pointer at run-rime Reply with quote



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
Back to top
Alf P. Steinbach
Guest





PostPosted: Mon Sep 29, 2003 9:33 pm    Post subject: Re: help using function pointer at run-rime Reply with quote



On 29 Sep 2003 14:30:13 -0700, [email]jdgamache (AT) hotmail (DOT) com[/email] (Jean-Daniel Gamache) wrote:

Quote:
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?


Back to top
Mike Wahler
Guest





PostPosted: Mon Sep 29, 2003 9:46 pm    Post subject: Re: help using function pointer at run-rime Reply with quote



"Jean-Daniel Gamache" <jdgamache (AT) hotmail (DOT) com> wrote

Quote:
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.

Quote:

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)

Quote:

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();

Quote:

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



Back to top
Gianni Mariani
Guest





PostPosted: Mon Sep 29, 2003 10:13 pm    Post subject: Re: help using function pointer at run-rime Reply with quote

Jean-Daniel Gamache wrote:
Quote:
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[i].name ) == 0 )
{
return Registry[i].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


Back to top
Yamin
Guest





PostPosted: Tue Sep 30, 2003 12:09 am    Post subject: Re: help using function pointer at run-rime Reply with quote


"Jean-Daniel Gamache" <jdgamache (AT) hotmail (DOT) com> wrote

Quote:
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



Back to top
Mike Wahler
Guest





PostPosted: Tue Sep 30, 2003 12:32 am    Post subject: Re: help using function pointer at run-rime Reply with quote


"Yamin" <absdfsd (AT) sdfdasfsd (DOT) com> wrote

Quote:

"Jean-Daniel Gamache" <jdgamache (AT) hotmail (DOT) com> wrote in message
news:dd1d9b4f.0309291330.3968234c (AT) posting (DOT) google.com...
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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.