读书人

读取变量调用同名函数解决方法

发布时间: 2012-03-11 18:15:39 作者: rapoo

读取变量调用同名函数
假设有一个函数名是Function,有没有办法当读取到“Function”这个string时,调用Function()这个函数
任何语言都可以,我只想知道其中的程序思想。感觉强制转换和指针都用不上,谢谢各位大侠了

[解决办法]
c/c++ dll动态加载~~~
java c#..反射~~
[解决办法]
if else

[解决办法]
在动态语言中这个非常简单. 比如 python 里面:

Python code
def Function():    print "Hello World!"s = "Function()"eval(s)
[解决办法]
你可以顶一个函数指针的结构体,包含有一个FunType;
typedef struct _ExempleFun_S
{
FunType_E m_eFunType;//这个事个枚举类型
Function_Fn m_fnFunction;
}ExempleFun_S;
[解决办法]
C/C++ code
Function(){......}void DoFunc(CString FuncStr){if (FuncStr==_T("Function")) Function();}
[解决办法]
类似
数据库的触发器
CPU的读数据断点
[解决办法]
探讨

很抱歉我没有完整地表达出我的意图。
假设有个string类型的变量Value,那无论Value里面的是什么,那么我希望都可以调用以这个string为函数名的函数

[解决办法]
楼上的方法都可以实现你说的嘛,呵呵
你弄个二维表,存放函数名字,函数指针,一关联不就得了
[解决办法]
建个函数地址表就可以了
[解决办法]
C/C++ code
void test1(){    cout << "test1" << endl;};void test2(){    cout << "test2" << endl;}int main(){    fun f1,f2;    f1 = test1;    f2 = test2;     map<int,fun> str_fun;     str_fun[1] = f1;    str_fun[2] = test2;    str_fun[1]();    str_fun[2]();    }
[解决办法]
探讨
……
test1
test2
请按任意键继续. . .



注意:要想用map<string,fun>好像要自己写个string类型的比较的东东

[解决办法]
函数指针。 最实用!

读书人网 >C语言

热点推荐