且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将参数动态传递给函数

更新时间:2022-06-27 02:41:32




你想做什么让我听起来像是ludacris。你的程序如何知道

如何处理它想要调用的每个函数?人工智能?

例如,我自己可以理解以下内容:


unsigned long CalculateCylinderCapacity(无符号长高度,无符号长

半径);

但是计算机怎么会知道如何使用它?

-JKop


What you want to do sound ludacris to me. How is your program going to know
how to work with each function it wants to call? Artificial Intelligence?
For instance, I myself can understand the following:

unsigned long CalculateCylinderCapacity(unsigned long height, unsigned long
radius);
But how the hell is the computer going to know how to work with that?
-JKop


请参阅 www.boost.org 上的boost :: function和boost :: bind。这可以帮助你更好地理解你想要做的事情的复杂性。


Jeff F


" Joel" < CI ****** @ gmail.com>在消息中写道

新闻:6f ************************** @ posting.google.c om ...
See boost::function and boost::bind at www.boost.org. This may help you to
better understand the intricacies involved in what you''re trying to do.

Jeff F

"Joel" <ci******@gmail.com> wrote in message
news:6f**************************@posting.google.c om...
大家好,

如果我已经表达了主题,我会原谅。

我想要做的就是调用c ++函数给出以下内容:
a。功能名称。这将用于获取该名称的重载函数的函数
描述符列表。函数
描述符将包含要调用的函数的地址,以及它必须采用的参数的描述。
b。参数列表。这将与每个函数描述符中的参数进行比较,以检查必须调用哪个函数,然后为desciptor提供参数,并请求调用该函数

说明:

................................ ...............
void SomeFunction(int a,int b)
{
//这是必须调用的函数。
}

void GenericFunction(string FunctionName,ParameterCollection
Paremeters)
{
FunctionDescriptorList Descriptors;
Descriptors.LoadFunctionDescriptors(FunctionName);

//参数对象包含要传递的参数列表。
//我将根据
//参数所需的参数验证集合中的参数。 DescriptorObject,和
使用
// DescriptorObject(描述名为
// SomeFunction的重载函数)来调用匹配SomeFunction,传递给它
//两个必需的整数参数(如上所述)。
}

............... ...............................

我不知道如何实现任何调用我在上面的评论中描述的功能。

我尝试过的一种方法是:

typedef void(* EllipsisFunction)(...);

void SomeFunction(int a,int b)
{
}
int main(void)
{
EllipsisFunction fn = (EllipsisFunction)SomeFunction;

fn(1,2);

.....等。
}
我可以使用这种性质的代码来实现我想要实现的东西(减去验证),除了事实是
我必须知道我实际上在调用时所调用的参数数量/>键入代码。

我可以使用的另一种方法是代替SomeFunction取两个整数,它可以简单地采用参数集合obje ct as
它是唯一的参数。我在上面描述的第二个GenericFunction在验证ParameterCollection对象时没有问题,而不是像MethodDescriptor
对象中所描述的那样对所需的参数进行验证,然后将ParameterCollection本身传递给
SomeFunction。然而,这意味着,SomeFunction的签名现在必须采用ParameterCollection。理想情况下,我希望如果它意味着采用两个整数,那么签名
应该反映出来。我知道它并不总是一个理想的世界;)

但我想做的是以某种方式推送所需的参数
(在ParametersCollection中指定)在我实际进行函数调用之前,在堆栈上。再次,请原谅我使用
非技术性词语,如某种方式。我已经达到了我在这个特定领域的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



2004年6月29日05:22:49 -0700, ci******@gmail.com (Joel)写道:


[snip]
On 29 Jun 2004 05:22:49 -0700, ci******@gmail.com (Joel) wrote:

[snip]
但我想要做的是以某种方式推送所需的参数
(在ParametersCollection对象中指定)到堆栈,在我实际进行函数调用之前。再次,请原谅我使用
非技术性词语,如某种方式。我已经达到了我在这个特定领域的C ++知识的程度。

那就是我现在所处的位置。这是一个冗长的帖子但是我想告诉你我一直在想这个问题。

问题是我现在看不到一个方法完全实现我在本文开头指定的理想。有没有办法可以实现?
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?




你不能只用C ++做到这一点......到现在为止,你已经完成了,

我想你必须意识到这一点。但是,如果你不介意在内联汇编代码中混合使用,你可以动态设置堆栈并调用该函数。
调用该函数。你必须要小心哪个调用约定

使用...在Windows上,例如有__stdcall和__cdecl。如果

你的被调用函数被声明为__stdcall,那么调用者在函数返回后不会清空堆栈(否则:它确实如此)。


-

Bob Hairgrove
No**********@Home.com