???????????????关于函数调用的简单问题

Iforgot 2003-09-07 11:45:23
就是如果我要在某个对象上调用函数,但是调用者没有函数名可用,参数有。

当然,调用者知道函数名。

具体就是这样:
一般我们会这样obj.FuctionName(....)
可是,我只有"FcutionName"的字符串和其参数(没有FuctionName了),怎么调用?

这样做,主要是我要根据动态的函数名去调用不同的函数。

有没有类似这样的方法可以用InvokeFuction(string fuction,...)类似的函数,如果有,在哪个类里?(我指C++ —— 最好是标准C++库或者windowsAPI实现也行(这个请指出lib和dll文件名、如何使用),但不要MFC的扩展,其他语言就不必了)。

谢谢各位指点了和up了!
...全文
76 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Iforgot 2003-09-08
  • 打赏
  • 举报
回复
很感谢各位的帮助和UP。

到时,我自己试试,如果能搞定自己的事,那就太感谢了。
Iforgot 2003-09-08
  • 打赏
  • 举报
回复
还有一个问题,我的函数参数问题怎么办?
好像写的那个都是无参函数的。
sevecol 2003-09-07
  • 打赏
  • 举报
回复
函数指针是你提供这样的服务的类自己保存的

调用者只要需要函数名就行了.
Iforgot 2003-09-07
  • 打赏
  • 举报
回复
怎么找函数指针? 能找到,我前面说的也就OK了啊!
SmileWolf 2003-09-07
  • 打赏
  • 举报
回复
有没有搞错,你这还叫“简单问题”啊?MFC扩展的OLE类你要是不使用权的话,那恐怕就得你自己动手照着OLE的自动化接口“画”一个了。
sevecol 2003-09-07
  • 打赏
  • 举报
回复
做一个分发器

也就是说首先将你的每个函数的指针和所对应函数名字符串保存在一个map里面
调用的时候用函数名字符串在map找到响应的函数指针,然后执行.
sevecol 2003-09-07
  • 打赏
  • 举报
回复
typedef void (obj::*p)();
void init()
{
pair<string,p> t_pair;

//第一个注册函数
t_pair,first=string("fun1");
t_pair.second==&(obj::fun1);

m_funcmap.insert(t_pair);

//下面同样的处理其他的函数
}
williamVII 2003-09-07
  • 打赏
  • 举报
回复
Pointer to function :fun1,fun2,fun3
The name of a function is the entry point of a function,which is
also an address.Just like the name of an array.

An example would make the point clear:
#include<iostream>

void fun1(void)
{
std::cout<<"Success!"<<std::endl;
}

int main(void)
{
void (*p)();
p=fun1;
(*p)();
return 0;
}

Iforgot 2003-09-07
  • 打赏
  • 举报
回复
//将obj的每个函数和函数名保存在m_funcmap中


怎么实现啊? 问题是我就不知道函数指针啊。

把具体的代码给出好吗?
谢谢!
williamVII 2003-09-07
  • 打赏
  • 举报
回复
Right!
Pointer to function combined with the name of the funtion.
Store them in a map.
Search for the name in the map,then invoke the corresponding funtion.
That's it!
sevecol 2003-09-07
  • 打赏
  • 举报
回复
class obj
{
public:
void fun1();
void fun2();
void fun3();
}

class server
{
typedef void (obj::*p)();
obj m_obj;
map<string,p> m_funmap;
public:
server(){init();}
void init()
{
//将obj的每个函数和函数名保存在m_funcmap中
}
void func(obj& objcect,string funcname)
{
//找到funcname所对应函数指针
(object.*p)();
}
}

}
Iforgot 2003-09-07
  • 打赏
  • 举报
回复
是啊! 那服务怎么提供?
tqrc 2003-09-07
  • 打赏
  • 举报
回复
up
jeary2210 2003-09-07
  • 打赏
  • 举报
回复
这个简单问题我不是很懂,帮忙UP一下:)

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧