转换为void的函数指针* [英] Casting function pointers to and from void*

查看:85
本文介绍了转换为void的函数指针*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在脚本语言和本机代码之间建立一个通用接口,

本机代码和解释器都将用C ++编写。解释器

可能会作为C / C ++的子集实现,因此将具有与C / C ++相同的语法。


不知何故,解释后的代码必须能够存储通用函数指针

,因为解释器无法提前知道每个可能的函数

签名。为了这个

的目的,我正在考虑将它们转换为void *。


解释的函数将提供通用函数指针,并且

函数原型作为文本。解释器将根据此函数原型准备堆栈本身

。我最终想要的是一个简单的

接口,用于解释器的用户,与本机代码用户的接口相同。


换句话说,如果函数原型是:

int FunctionName(int N);

我希望使用我的解释器来指定:

int Result = FunctionName(56);

调用本机代码函数时。


什么样的C ++语法将需要这个吗?

I want to make a generic interface between a scripting language and native code,
the native code and the interpreter will both be written in C++. The interpreter
will probably be implemented as a subset of C/C++, thus will have the same
syntax as C/C++.

Somehow the interpreted code must be able to store generic function pointers
because there is no way for the interpreter to know every possible function
signature in advance. I was thinking about casting them to void* for this
purpose.

The interpreted function will be provided with the generic function pointer, and
the function prototype as text. The interpreter will prepare the stack itself
based on this function prototype. What I want to end up with is a simple
interface for users of the interpreter that is identical to the interface for
the native code users.

In other words if the function prototype is:
int FunctionName(int N);
I want uses of my interpreter to be able to specify:
int Result = FunctionName(56);
when they are calling the native code functions.

What kind of C++ syntax will be needed for this?

推荐答案



Peter Olcott写道:

Peter Olcott wrote:

我想在脚本语言和本机代码之间建立一个通用接口,

本机代码和解释器都将用C ++编写。解释器

可能会作为C / C ++的子集实现,因此将具有与C / C ++相同的语法。


不知何故,解释后的代码必须能够存储通用函数指针

,因为解释器无法提前知道每个可能的函数

签名。为了这个

的目的,我正在考虑将它们转换为void *。


解释的函数将提供通用函数指针,并且

函数原型作为文本。解释器将根据此函数原型准备堆栈本身

。我最终想要的是一个简单的

接口,用于解释器的用户,与本机代码用户的接口相同。


换句话说,如果函数原型是:

int FunctionName(int N);

我希望使用我的解释器来指定:

int Result = FunctionName(56);

调用本机代码函数时。


什么样的C ++语法这需要吗?
I want to make a generic interface between a scripting language and native code,
the native code and the interpreter will both be written in C++. The interpreter
will probably be implemented as a subset of C/C++, thus will have the same
syntax as C/C++.

Somehow the interpreted code must be able to store generic function pointers
because there is no way for the interpreter to know every possible function
signature in advance. I was thinking about casting them to void* for this
purpose.

The interpreted function will be provided with the generic function pointer, and
the function prototype as text. The interpreter will prepare the stack itself
based on this function prototype. What I want to end up with is a simple
interface for users of the interpreter that is identical to the interface for
the native code users.

In other words if the function prototype is:
int FunctionName(int N);
I want uses of my interpreter to be able to specify:
int Result = FunctionName(56);
when they are calling the native code functions.

What kind of C++ syntax will be needed for this?



我不明白你的问题,但也许提升::功能

会帮助你。

I don''t understand your problem as stated but maybe boost::function
will aid you.


On Fri,2007年1月5日10:21:46 -0600,Peter Olcott写道:
On Fri, 05 Jan 2007 10:21:46 -0600, Peter Olcott wrote:

我想要在脚本语言和本机代码之间建立一个通用接口,

本机代码和解释器都将用C ++编写。解释器

可能会作为C / C ++的子集实现,因此将具有与C / C ++相同的语法。


不知何故,解释后的代码必须能够存储通用函数指针

,因为解释器无法提前知道每个可能的函数

签名。为了这个

的目的,我正在考虑将它们转换为void *。


解释的函数将提供通用函数指针,并且

函数原型作为文本。解释器将根据此函数原型准备堆栈本身

。我最终想要的是一个简单的

接口,用于解释器的用户,与本机代码用户的接口相同。


换句话说,如果函数原型是:

int FunctionName(int N);

我希望使用我的解释器来指定:

int Result = FunctionName(56);

调用本机代码函数时。


什么样的C ++语法这需要吗?
I want to make a generic interface between a scripting language and native code,
the native code and the interpreter will both be written in C++. The interpreter
will probably be implemented as a subset of C/C++, thus will have the same
syntax as C/C++.

Somehow the interpreted code must be able to store generic function pointers
because there is no way for the interpreter to know every possible function
signature in advance. I was thinking about casting them to void* for this
purpose.

The interpreted function will be provided with the generic function pointer, and
the function prototype as text. The interpreter will prepare the stack itself
based on this function prototype. What I want to end up with is a simple
interface for users of the interpreter that is identical to the interface for
the native code users.

In other words if the function prototype is:
int FunctionName(int N);
I want uses of my interpreter to be able to specify:
int Result = FunctionName(56);
when they are calling the native code functions.

What kind of C++ syntax will be needed for this?



你不能将指向函数的指针转换为指向对象的指针。

reinterpret_cast可以在指向不同的指针之间进行转换功能

only。

You can''t cast a pointer to a function to a pointer to an object.
reinterpret_cast can convert between two pointers to different functions
only.




" Philipp Reh" < se ** @ sefi.dewrote in message

news:45 ******** @ news.uni-siegen.de ...

"Philipp Reh" <se**@s-e-f-i.dewrote in message
news:45********@news.uni-siegen.de...

On Fri,2007年1月5日10:21:46 -0600,Peter Olcott写道:
On Fri, 05 Jan 2007 10:21:46 -0600, Peter Olcott wrote:

>我想制作一个通用接口在脚本语言和本机代码之间,本机代码和解释器都将用C ++编写。
解释器可能会作为C / C ++的子集实现,因此将具有与C / C ++相同的语法。

某种方式解释的代码必须能够存储通用函数指针
因为解释器无法事先知道每个可能的函数
签名。为了这个目的,我正在考虑将它们转换为void *。

解释的函数将提供通用函数指针,

函数原型为文本。解释器将根据此函数原型准备堆栈本身。我想最终得到的是一个简单的解释器用户界面,与本机代码用户的界面相同。

换句话说,如果功能原型是:
int FunctionName(int N);
我希望使用我的解释器来指定:
int Result = FunctionName(56);
当他们调用时本机代码函数。

这需要什么样的C ++语法?
>I want to make a generic interface between a scripting language and native
code,
the native code and the interpreter will both be written in C++. The
interpreter
will probably be implemented as a subset of C/C++, thus will have the same
syntax as C/C++.

Somehow the interpreted code must be able to store generic function pointers
because there is no way for the interpreter to know every possible function
signature in advance. I was thinking about casting them to void* for this
purpose.

The interpreted function will be provided with the generic function pointer,
and
the function prototype as text. The interpreter will prepare the stack itself
based on this function prototype. What I want to end up with is a simple
interface for users of the interpreter that is identical to the interface for
the native code users.

In other words if the function prototype is:
int FunctionName(int N);
I want uses of my interpreter to be able to specify:
int Result = FunctionName(56);
when they are calling the native code functions.

What kind of C++ syntax will be needed for this?



你不能将指向函数的指针转换为指向对象的指针。

reinterpret_cast可以在指向不同的两个指针之间转换功能
仅限


You can''t cast a pointer to a function to a pointer to an object.
reinterpret_cast can convert between two pointers to different functions
only.



有没有标准的C ++方法来完成我需要完成的工作?

Is there any standard C++ way to accomplish what I need to accomplish?


这篇关于转换为void的函数指针*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆