473,385 Members | 1,930 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,385 software developers and data experts.

Is it possible to call a function which name is given by a string?

Is it possible to call a function which name is given by a string?

Let assume that I created a program which call some functions for example

void f1(void),
void f2(void),
void f3(void).

After some time, I added new function void f4(void).

However my previous program doesn't know that the function f4 exist.
It is possible to call this function without recompiling the whole program?

It is possible to create a procedure, lets say

void CallFunctionByName(char *FunctionName);

which will be called function which is added to the program in the future?

For example the instruction

CallFunctionByName("f4");

Will call the function "f4", if the function is not linked by the program
then error message will be given.

Is it possible to do that in C++?

Regards,

Andrzej
Jul 23 '05 #1
7 2324
well, i think a function pointer could do that

Jul 23 '05 #2
On 2005-05-23, Andrzej <cp***************@test.com> wrote:
Is it possible to call a function which name is given by a string?

Let assume that I created a program which call some functions for example

void f1(void),
void f2(void),
void f3(void).

After some time, I added new function void f4(void). However my previous program doesn't know that the function f4 exist.
It is possible to call this function without recompiling the whole program?


You can call functions indirectly by using function pointers. Not sure if
that solves your problem or not. Most platforms support dynamic loading --
which makes it possible to load libraries at run time. Further discussion
of this is off-topic here (see your OS newsgroup), but this might be an
option worth pursuing.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3
Andrzej wrote:

Is it possible to call a function which name is given by a string?


Not directly.
But function pointers open up a way:
Create a table which maps a functions name to a function pointer.

Short scetch:

#include <iostream>
#include <string>
#include <map>

typedef void (*FnctPtr) (void);
typedef std::map< std::string, FnctPtr > LookUpTable;

class Dispatcher
{
public:
void Register( const std::string& Name, FnctPtr Function );
void Dispatch( const std::string& Name );

private:
LookUpTable m_Functions;
};

void Dispatcher::Register( const std::string& Name, FnctPtr Function )
{
m_Functions[Name] = Function;
}

void Dispatcher::Dispatch( const std::string& Name )
{
LookUpTable::iterator it = m_Functions.find( Name );
if( it != m_Functions.end() )
it->second();
else
std::cout << "Error: Function \'" << Name << "\' not in dispatch table\n";
}

void f1()
{
std::cout << "This is f1\n";
}

void f2()
{
std::cout << "This is f2\n";
}

int main()
{
Dispatcher Disp;

Disp.Register( "f1", f1 );
Disp.Register( "f2", f2 );

Disp.Dispatch( "f1" );
Disp.Dispatch( "f2" );
Disp.Dispatch( "f3" );

return 0;
}
--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad.at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 23 '05 #4
I agree with Teddy that a function pointer is a nice way to solve this
problem of yours.

First of all you would need some kind of "function registry" and a
function which your new functions can call to register themselves.
It might look something like this:

std::map<std::string, void*) registry;

void registerFunction(std::string functionName, void* fP)
{
registry[functionName] = fP;
}

And your callFunctionByName would then look like:

void callFunctionByName(std::string functionName)
{
void (*fx)() = registry[functionName];
fx();
// Throw some exceptions if function is not registred...
}

And here goes an example which shows how to use it:

void exampleFunction() { // Do something good }

int main()
{
registerFunction("example", &exampleFunction);
callFunctionByName("example);
}

Hope this helps
// eKIK

P.S.
This code is straight out of my head, so it probably won't work
cut-n-paste style...but hopefully the basics are right at least.
D.S.

Jul 23 '05 #5
> Is it possible to call a function which name is given by a string?

Maybe, but you really don't want to do this unless there is absolutely
no alternative solution to your problem. Just not wanting to recompile
the whole program is not a good enough reason to resort to this, IMHO.

On Linux you can use h=dlopen(NULL) followed by dlsym(h,"funcname").
The key thing is that passing NULL means to look up the symbol in the
main program. Cast the result of dlsym to a function pointer and then
call it. Similar things are possible on other Unix variants though not
all support passing NULL to dlopen. On Windows there is something
called LoadLibrary which does some of what dlopen does, but I don't
know if it has a way to access the main program's symbols.

For a bit more info, have a look at
http://www.isotton.com/howtos/C++-dl...ini-HOWTO.html

--Phil.

Jul 23 '05 #6
>> Is it possible to call a function which name is given by a string?
on Windows there is something called LoadLibrary


You need GetModuleHandle(NULL) followed by GetProcAddress("function
name")

Jul 23 '05 #7
Thank you very much.

Andrzej
Jul 23 '05 #8

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

Similar topics

22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
3
by: Mariusz | last post by:
I want to write function to call another function which name is parameter to first function. Other parameters should be passed to called function. If I call it function('f1',10) it should call...
36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
13
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a...
11
by: vbgunz | last post by:
Hello all, I am just learning Python and have come across something I feel might be a bug. Please enlightenment me... The following code presents a challenge. How in the world do you provide an...
4
by: Pritcham | last post by:
Hi all I've got a number of classes already developed (basic entity classes) like the following: Public Class Contact Private _firstname as String Private _age as Integer Public Property...
2
by: Chicken15 | last post by:
Hi Group. First of all I'm sorry for asking (maybe) such easy questions. But I'm quite stuck now and couldn't come up with a solution by using my C# book or googling. So it would be nice if...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.