如何使用 SWIG 返回指向用户定义的类对象的指针
我有以下由 swig 包装的代码:
int
cluster::myController(controller*& _controller) {
_controller = my_controller;
return 0;
}
controller
有一个私有构造函数。
使此类不抛出异常的正确咒语是什么?
public static void main(String argv[]) {
controller c = null;
int r = dan_cluster.theCluster().myController(c);
System.out.println(r);
System.out.println(c.getUUID());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要发布您收到的异常,这将会很有帮助,但我不相信您尝试做的事情是可能的。 在 C++ 中,您可以通过引用传递指针,从而对传递给函数的指针变量进行更改,但在 Java 中没有等效的东西。 您应该检查生成的包装器,但我的猜测是您的 C++ 代码被包装为以下内容:
...这显然不会执行您想要的操作,从而在您尝试 getUUID() 时导致 NullPointerException。
假设我是对的,最好的解决办法是 myController() 仅返回一个控制器*。 如果您确实需要整数,请考虑返回 std::pair (注意:包装 stl 的任何部分都需要一些技巧,请参阅 文档)。
It would be helpful if you were to post the exception you're getting, but I don't believe that what you're trying to do is possible. In C++ you can pass a pointer by reference and so make changes to a pointer variable passed to a function, but in Java there is nothing equivalent. You should check the generated wrapper, but my guess is that your C++ code is being wrapped as something like:
...which clearly won't do what you want, leading to a NullPointerException when you try to getUUID().
Assuming I'm right, the best fix is for myController() to just return a controller*. If you really need the integer, consider returning a std::pair (note: wrapping any part of the stl requires some delicacy, consult the documentation).