问题描述
大家好,
请原谅我,如果我已经表达了主题内容的话。
我想要的是什么在给定以下内容的情况下调用c ++函数:
a。功能名称。这将用于获取该名称的重载函数的函数列表
描述符。函数
描述符将包含要调用的函数的地址,并且
描述它必须采用的参数。
b。参数列表。这将与
中的每个函数描述符中的参数进行比较,以检查哪个函数必须是
调用,然后desciptor将被赋予参数,并且
请求调用该函数
为了说明:
............ ...................................
void SomeFunction(int a,int b)
{
//这是必须调用的函数。
}
void GenericFunction(string FunctionName,ParameterCollection
Paremeters)
{
FunctionDescriptorList Descriptors;
Descriptors.LoadFunctionDescriptors( FunctionName);
//参数对象包含要传递的参数列表。
//我会验证集合中的参数与DescriptorObject指定的
//参数相关,并且
使用
// DescriptorObject(whic) h描述了名为
// SomeFunction的重载函数来调用匹配的SomeFunction,传递给它
//两个必需的整数参数(如上所述)。
}
................................. ..........
我不知道如何实现我的任何调用功能
descibed in上面的评论。
我尝试过的一种方法是:
typedef void(* EllipsisFunction)(...);
void SomeFunction(int a,int b)
{
}
int main (void)
{
EllipsisFunction fn =(EllipsisFunction)SomeFunction;
fn(1,2);
.....等。
}
我可以使用这种性质的代码来实现我的某些东西我想要实现(减去验证)
,除了
再次这个事实我必须知道我实际上打电话时调用的参数数量/>
键入代码。
我可以使用的另一种方法是,而不是SomeFunction取两个整数的
,它可能只需要一个参数集合对象为
它是唯一的参数。我在上面描述的第二个GenericFunction
在使用MethodDescriptor中描述的所需参数验证ParameterCollection对象时没有问题
对象,然后将ParameterCollection本身传递给
SomeFunction。然而,这意味着,
SomeFunction的签名现在必须采用ParameterCollection。理想情况下,我希望
更喜欢如果它意味着取两个整数,那么签名
应该反映出来。我知道它并不总是一个理想的
世界;)
但我想做的是以某种方式推送所需的参数
(在ParametersCollection对象中指定)到堆栈上,然后我才实现
实际进行函数调用。再次,请原谅我使用
非技术性词语,如某种方式。我已经达到了我在这个特定领域的b $ b C ++知识的程度。
这就是我现在的位置。这是一个冗长的帖子但是我想要告诉你我是怎么想的。
问题在于我现在还没有看到完全实现的方法
这是我在本文开头指定的理想。
有没有办法可以实现?
致以最诚挚的问候,
cirquitz
Hi all,
Forgive me if I''ve expressed the subject line ill.
What I''m trying to do is to call a c++ function given the following:
a. A function name. This would be used to fetch a list of function
descriptors for the overloaded functions of that name. A function
descriptor would contain the address of the function to be called, and
a description of the parameters that it must take.
b. A list of parameters. This would be compared to the parameters in
each of the function descriptors to check which function must be
called, and then the desciptor would be given the parameters, and
requested to invoke that function
To illustrate:
...............................................
void SomeFunction (int a, int b)
{
// This is the function that must be invoked.
}
void GenericFunction (string FunctionName, ParameterCollection
Paremeters)
{
FunctionDescriptorList Descriptors;
Descriptors.LoadFunctionDescriptors (FunctionName);
// The Parameters object contains a list of parameters to be
passed.
// I would validate the parameters in the collection against the
// parameters required as specified by the DescriptorObject, and
use the
// DescriptorObject (which describes overloaded functions named
// SomeFunction) to call the matching SomeFunction, passing to it
// the two required integer parameters (as above).
}
...............................................
I am not sure how to achieve any of the invoking functionality I have
descibed in the comments above.
One approach I have tried is:
typedef void (* EllipsisFunction) (...);
void SomeFunction (int a, int b)
{
}
int main (void)
{
EllipsisFunction fn = (EllipsisFunction) SomeFunction;
fn (1, 2);
.....etc.
}
I could use code of this nature to effect something of what I am
trying to achieve (minus the validation), except for the fact that
again I must know the number of parameters that I call when I actually
type the code.
Another approach that I can use is that instead of SomeFunction taking
two integers, it could simply take a parameter collection object as
it''s sole parameter. The second GenericFunction that I described above
would then have no problems in validating a ParameterCollection object
against the required parameters as described in a MethodDescriptor
object, and then in passing the ParameterCollection itself to
SomeFunction. This implies, however, that the signature of
SomeFunction would now have to take a ParameterCollection. Ideally, I
would prefer that if it means to take two integers, then the signature
should reflect that. I am aware tho that it isn''t always an ideal
world ;)
But what I want to do is somehow to push the required parameters
(specified in a ParametersCollection object) onto the stack, before I
actually make the function call. Again, forgive me for using
non-technical words like "somehow". I have reached the extent of my
C++ knowledge in this particular area.
And that''s where I am at present. It''s a bit of a lengthy post but I
wanted to let you know how I''ve been thinking about this.
The problem is that I do not see right now a way to completely fulfill
the ideal that I have specified at the beginning of this post. Is
there a way that it could be accomplished?
With best regards,
cirquitz
推荐答案
你不能只用C ++做到这一点......到现在为止,你已经完成了,
我想你必须意识到这一点。但是,如果你不介意在内联汇编代码中混合使用,你可以动态设置堆栈并调用该函数。
调用该函数。你必须要小心哪个调用约定
使用...在Windows上,例如有__stdcall和__cdecl。如果
你的被调用函数被声明为__stdcall,那么调用者在函数返回后不会清空堆栈(否则:它确实如此)。
-
Bob Hairgrove
这篇关于将参数动态传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!