简体   繁体   English

为什么将此功能视为委托?

[英]Why is this function seen as a delegate?

In an attempt to wrap a C function taking callbacks, I encountered the problem of member functions being seen as delegates. 在尝试包装带有回调的C函数时,我遇到了将成员函数视为委托的问题。 The C function wouldn't take delegates, so I settled on something else instead: C函数不会接受委托,因此我选择了其他方法:

extern(C) void onMouse(void delegate(int, int, int) nothrow callback)
{
    glfwSetMouseButtonCallback(handle,
        function void (GLFWwindow* h, int button, int action, int mods) nothrow
        {
            callback(button, action, mods);
        }
    );
}

As you can see, I'm passing to the callback setting function a function literal calling a delegate (which would be the member function I pass here). 如您所见,我正在向回调设置函数传递一个调用委托的函数文字(这将是我在此处传递的成员函数)。

However, it doesn't end up as I expect: 但是,它并没有像我期望的那样结束:

Error: function pointer glfwSetMouseButtonCallback (GLFWwindow*, extern (C) void function(GLFWwindow*, int, int, int) nothrow) is not callable using argument types (GLFWwindow*, void delegate(GLFWwindow* h, int button, int action, int mods) nothrow @system)

In the error, it shows the second parameter as being as the type void delegate . 在错误中,它显示第二个参数为类型void delegate

So. 所以。 My question is: why does this happen? 我的问题是:为什么会这样? As you can clearly see, it says function void in the code. 如您所见,代码中的function void

NOTE: I've seen this: Pass delegates to external C functions in D . 注意:我已经看到了: 将委托传递给D中的外部C函数 The solution is apparently a hack. 解决方案显然是黑客。 But I will try it if I cannot find a workaround on the internet. 但是,如果无法在Internet上找到解决方法,我会尝试一下。

Even though you declare it as function, it cannot be. 即使您将其声明为函数,也不能。 It relies on the parameter you pass to onMouse . 它依赖于您传递给onMouse的参数。 That parameter is a local variable, and accessing them in a function body makes them a delegate. 该参数是一个局部变量,在函数体中对其进行访问将使它们成为委托。 All you can do is change the parameter to function and pass it with &callback (you'd need to add GLFWwindow as parameter then). 您所要做的就是将参数更改为function并通过&callback传递它(然后您需要添加GLFWwindow作为参数)。

Alternatively, you can create a global list in which this callback puts events which you can then process in your main loop. 另外,您可以创建一个全局列表,此回调将在其中放入事件,然后可以在主循环中处理这些事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能将函数指针视为指向函数的指针? - Why function pointer isn't seen as point to function? 使用函数返回值而不先存储它-为什么很少见? - Use function return value without storing it first - why is this rarely seen? 调用者看不到函数中的变量已更改? - Variable changed in function not seen by caller? Linux内核 - 为什么System.map中的函数地址是实时看到的地址之前的一个字节? - Linux Kernel - why a function's address in System.map is one byte preceding its address as seen in real time? 使用C#4.0中的匿名函数,为什么我无法定义内联委托 - With anonymous function in C# 4.0 why can't I define a delegate inline 我的共享库的c函数在库外看不到 - c function of my shared lib is not seen out of the lib GTK:将函数调用委托给主线程 - GTK: Delegate function call to main thread 在C#中使用带有函数指针和委托的PInvoke - Using PInvoke in C# with function pointer and delegate 将元帅委托给函数指针:内存损坏 - marshal delegate to function pointer: memory corrupt 在 FSEvent 回调中访问 app delegate function - Access app delegate function in FSEvent callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM