473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

help using function pointer at run-rime

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
Jul 19 '05 #1
5 2135
On 29 Sep 2003 14:30:13 -0700, jd*******@hotmail.com (Jean-Daniel Gamache) wrote:
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?

Jul 19 '05 #2
"Jean-Daniel Gamache" <jd*******@hotmail.com> wrote in message
news:dd**************************@posting.google.c om...
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
Jul 19 '05 #3
Jean-Daniel Gamache wrote:
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

Jul 19 '05 #4

"Jean-Daniel Gamache" <jd*******@hotmail.com> wrote in message
news:dd**************************@posting.google.c om...
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
Jul 19 '05 #5

"Yamin" <ab*****@sdfdasfsd.com> wrote in message
news:vQ****************@news20.bellglobal.com...

"Jean-Daniel Gamache" <jd*******@hotmail.com> wrote in message
news:dd**************************@posting.google.c om...
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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
2
by: plank | last post by:
Hey Peeps, Ok here is my situation.. I have a Java applet which allows the user to select files and upload them to the server. The applet converts the file to Base64 and then POSTS the data to an...
22
by: Dave Cooke | last post by:
Hi I am very new to C. I am trying to figure out how to initialize a struct in my main program. The struct is declared in anouther header file like this... typedef struct ln { int key; int data;...
2
by: OpticTygre | last post by:
Ok, so I've been trying and trying to learn about delegates, but I still can't grasp the concept of the advantages. Why use delegate functions instead of calling the function directly? In other...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
2
by: Marcus Kwok | last post by:
I have processing code (I'll call it the "model") written in native unmanaged pure C++, and I have put a GUI on top of it written using Windows Forms (.NET 1.1). The GUI is used to set the...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
0
by: Mike S | last post by:
I've seen examples of using the CallWindowProc Windows API function to call functions through their addresses in VB6 -- a clever workaround to emulate C-like function pointer semantics. A...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.