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

A

Andrzej

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
 
D

Donovan Rebbechi

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,
 
K

Karl Heinz Buchegger

Andrzej said:
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: (e-mail address removed) Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
 
E

eKIK

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.
 
P

phil_gg04

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++-dlopen-mini-HOWTO/C++-dlopen-mini-HOWTO.html

--Phil.
 
P

phil_gg04

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")
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top