如何使用字符串调用函数? [英] How to call a function just using a string?

查看:145
本文介绍了如何使用字符串调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我有一个问题,即如何使用字符串调用函数。

例如

有一个.cpp文件名为a.cpp.There是一些函数:: fun1()

fun2()fun3()。

我有另一个函数void funcall(char * pch )。如果我传递了一个

参数char * p1 =" fun1" 。如何使用我传递的

字符串fun1来调用函数fun1()。

Hi All!
I have a question that how to call a function just using a string.
For example
There is a .cpp file named a.cpp.There are some functions::fun1()
fun2() fun3().
I have another fucntion void funcall( char *pch). if I pass a
argument char* p1="fun1" .How do I call the function fun1() using that
string "fun1"that I pass.

推荐答案

dolphin写道:
dolphin wrote:

我有一个问题,即如何使用字符串调用函数。

例如

有一个名为a.cpp的.cpp文件。有一些函数:: fun1()

fun2()fun3()。

我有另一个函数无效funcall(char * pch)。如果我传递了一个

参数char * p1 =" fun1" 。如何使用我传递的

字符串fun1来调用函数fun1()。
I have a question that how to call a function just using a string.
For example
There is a .cpp file named a.cpp.There are some functions::fun1()
fun2() fun3().
I have another fucntion void funcall( char *pch). if I pass a
argument char* p1="fun1" .How do I call the function fun1() using that
string "fun1"that I pass.



我不知道在便携式C ++中有什么方法可以做到这一点。


一些解决方案:


*让funcall为a.cpp添加一些代码,允许选择一个函数

并编译并执行该文件,

*动态加载文件,运行时绑定所需的函数和

调用它们,

* make funcall是C ++解释器的接口,并运行它a.cpp,

调用所需的功能,

* make funcall发送邮件给你生气的牛犊,指示他

编辑,编译并运行该文件,以便所需的功能将被执行


I''m not aware of any way to do this in portable C++.

Some "solutions":

* Have funcall add some code to a.cpp that allows to select a function
and compile and execute the file,
* dynamically load the file, runtime-binding the desired function(s) and
invoke them,
* make funcall an interface to a C++ interpreter, and run it on a.cpp,
calling the desired function,
* make funcall send a mail to your annoyed cow-orker, instructing him to
edit, compile and run the file so that the desired function will be
executed.


12月4日下午12:37,dolphin< jdxyw2 ... @ gmail.comwrote:
On Dec 4, 12:37 pm, dolphin <jdxyw2...@gmail.comwrote:

大家好!

我有一个问题,即如何使用字符串调用函数。

例如

有一个名为a.cpp的.cpp文件。一些函数:: fun1()

fun2()fun3()。

我有另一个fucntion void funcall(char * pch)。如果我传递了一个

参数char * p1 =" fun1" 。如何使用我传递的

字符串fun1来调用函数fun1()。
Hi All!
I have a question that how to call a function just using a string.
For example
There is a .cpp file named a.cpp.There are some functions::fun1()
fun2() fun3().
I have another fucntion void funcall( char *pch). if I pass a
argument char* p1="fun1" .How do I call the function fun1() using that
string "fun1"that I pass.



#include< map>

#include< string>


typedef std :: map< std :: string,void(*)(void)FuncMapTypeBase;


struct FuncMapType:

FuncMapTypeBase

{

FuncMapType(){

(* this)[" fun1"] =& fun1;

(* this)[" ; fun2"] =& fun2;

(* this)[" fun3"] =& fun3;

}

} ;


const FuncMapType& funxns(){

static FuncMapType funcs;

return funcs;

};


void funcall(const std :: string& str){

(*(funcxns()[str]))();

};


问候,

FM

#include <map>
#include <string>

typedef std::map<std::string,void(*)(void)FuncMapTypeBase;

struct FuncMapType :
FuncMapTypeBase
{
FuncMapType(){
(*this)["fun1"]=&fun1;
(*this)["fun2"]=&fun2;
(*this)["fun3"]=&fun3;
}
};

const FuncMapType &funxns(){
static FuncMapType funcs;
return funcs;
};

void funcall(const std::string& str){
(*(funcxns()[str]))();
};

regards,
FM


12月4,2:37 pm,dolphin< jdxyw2 ... @ gmail.comwrote:
On Dec 4, 2:37 pm, dolphin <jdxyw2...@gmail.comwrote:

大家好!

我有一个问题如何使用字符串调用函数。

例如

有一个名为a.cpp的.cpp文件。有一些函数:: fun1()

fun2()fun3()。

我有另一个函数void funcall(char * PCH)。如果我传递了一个

参数char * p1 =" fun1" 。如何使用我传递的

字符串fun1来调用函数fun1()。
Hi All!
I have a question that how to call a function just using a string.
For example
There is a .cpp file named a.cpp.There are some functions::fun1()
fun2() fun3().
I have another fucntion void funcall( char *pch). if I pass a
argument char* p1="fun1" .How do I call the function fun1() using that
string "fun1"that I pass.



不能完全满足您的要求但类似的东西 -

dlopen,dlsym和dlclose。这是linux / unix特有的。对于Windows -

你有LoadLibrary和GetProcAddress。对于其他平台,你需要找到替代品。

Does not do exactly what you are asking for but something similar -
dlopen, dlsym and dlclose. This is linux/unix specific. For windows -
you have LoadLibrary and GetProcAddress. For other platforms, you
would need to find alternatives.


这篇关于如何使用字符串调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆