带参数的Void *函数(Void* function with a parameter)

编程入门 行业动态 更新时间:2024-10-26 22:16:11
带参数的Void *函数(Void* function with a parameter)

对于我正在尝试编写的应用程序,我需要能够在interface编写GLEnable(GL_REPEAT)工作)。 一旦用户这样做,系统应该使用正确的参数调用该函数。

为此,我使用以下变量来获取正确的函数:

std::map<std::string, void(*)(GLenum)> glEnableDisable;

并通过以下方式存储数据:

glEnableDisable["glEnable"] = glEnable; glEnableDisable["glDisable"] = glDisable;

(注意,上面给出警告“'='不能从'void(__ stdcall *)(GLenum)'转换为'void(__ cdecl *)(GLenum)'”)

最后,通过以下方式调用该函数:

((void (*)(GLenum)) Main.glEnableDisable[SplittedUp[0]])(Parameter);

现在,我想知道我做错了什么。 我没有参数这个工作正常,但我真的需要参数。

提前致谢,

乔伊

For an application I'm trying to write I need to be able to write GLEnable(GL_REPEAT) in an interface (got this working). Once the user does that, the system should call the function with the correct parameter.

For this, I am using the following variable to get the correct function:

std::map<std::string, void(*)(GLenum)> glEnableDisable;

And storing data in it through:

glEnableDisable["glEnable"] = glEnable; glEnableDisable["glDisable"] = glDisable;

(Note that the above gives a warning "'=' cannot convert from 'void (__stdcall *)(GLenum)' to 'void (__cdecl *)(GLenum)'")

Finally, the function is called through:

((void (*)(GLenum)) Main.glEnableDisable[SplittedUp[0]])(Parameter);

Now, I wonder what I'm doing wrong. I had this working fine without parameters, but I really need parameters for this.

Thanks in advance,

Joey

最满意答案

改变这个:

std::map<std::string, void(*)(GLenum)> glEnableDisable;

对此:

std::map<std::string, std::function<void(GLenum)>> glEnableDisable;

并且再也不用担心召唤约定了。 std::function能够存储任何函数或可调用对象,无论其调用约定如何。

不需要演员:

Main.glEnableDisable[SplittedUp[0]])(Parameter);

Change this:

std::map<std::string, void(*)(GLenum)> glEnableDisable;

to this:

std::map<std::string, std::function<void(GLenum)>> glEnableDisable;

and never worry about calling conventions again. An std::function is able to store any function or callable object, whatever its calling convention is.

Cast is not needed:

Main.glEnableDisable[SplittedUp[0]])(Parameter);

更多推荐

本文发布于:2023-07-20 11:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数   parameter   Void   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!