我已经发布这个问题 https://stackoverflow.com/questions/15812911/c-programatically-call-correct-function-parameters. SpongeBobFan
说答案是反思。我发现增强反射 http://bytemaster.github.com/boost_reflect/index.html我想知道我到底如何使用该库或另一个 C++ 反射库来实现这一点。请解释你的答案,因为我不能只看一眼代码就知道发生了什么。我的问题是这样的:
好的,我有一个问题。假设我有这段代码:
int myfunc(int arg-a, int arg-b);
int mywrapperfunc(obj a, obj b);
mywrapperfunc
应该包裹myfunc
. mywrapperfunc
丢弃第一个
参数并采用第二个,它是一个数组。然后我将数组项用作
参数。但说我不知道有多少个参数myfunc
需要,我也不知道
数组类型对象中有多少项(b
)。我将如何以编程方式调用myfunc
参数数量正确吗?
移交的参数数量将与项目中的项目数量相同
数组类型对象。
EDIT: arg-a
and arg-b
应该来自数组类型
目的。我将对象拆分为参数。
编辑:好吧,好吧,我试图用一些涉及的意义来包装 Python C API,隐藏大部分内容
后台工作。
你看过吗Reflex http://root.cern.ch/drupal/content/reflex?该工具使用GCC-XML http://gccxml.github.io/HTML/Index.html创建一个类似于 Java 中的反射库。
这是我的初步看法(我目前没有太多空闲时间来进一步清理它):
// Use the Reflex library to look up "myfunc" reflectively
Type t = Type::ByName("myfunc");
// The Reflex type 'Type" has a "FunctionParameterSize()" that tells
// you how many args in a function
size_t num_params = t.FunctionParameterSize();
// Use this information to call "myfunc" with objects from "b"
如果 Reflex 只提供对类成员的调用功能,那么您可以myfunc()
一个静态类成员,并尝试这样的事情:
// Load MyClass reflectively (where MyClass contains a static member function "myfunc")
Type t = Type::ByName("MyClass");
// Find the member function "myfunc"
Member m = t.MemberByName("myfunc");
// Pack the parameters from "b" into a vector
// (Assumes "b" has indexing operators, and a "length()" function)
std::vector params(&b[0], &b[b.length()-1]);
// Invoke the method "myfunc" reflectively
m.Invoke(params);
你必须摆弄这个,我不确定我是否正确地进行了参数传递,等等,但希望这能给你一些新的想法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)